﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.ModelBinding;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
//using static netDxf.Entities.HatchBoundaryPath;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;

namespace TestDxfDocument
{
    class fun

    {
        static int value = 50;
        public static Vector3 Tothree(Vector2 v2)  //二维点to三维点
        {
            return new Vector3(v2.X, v2.Y, 0);
        }

        /// <summary>
        /// 三维点转换成二维点，z方向舍去
        /// </summary>
        /// <param name="v3"></param>
        /// <returns></returns>
        public static Vector2 toTwo(Vector3 v3)
        {
            return new Vector2(v3.X, v3.Y);
        }

        public static Vector2 rotatePoint_Changed(Vector2 point1, Vector2 point2, double angle)  //旋转点(一点绕另一点旋转)
                                                                                                 //point1是参照点，point2是被旋转的点
        {
            double pi = Math.PI;
            double r = fun.CalDistance(point1, point2);
            double x = Math.Abs(Math.Abs(point1.X) - Math.Abs(point2.X));
            double y = Math.Abs(Math.Abs(point1.Y) - Math.Abs(point2.Y));
            double include_angle = fun.CalAngle(point1, point2);
            double rotate_angle = angle * pi / 180 + include_angle;

            Vector2 point3 = fun.Polar(point1, rotate_angle, r);

            return point3;
        }
        //hp1点，ang方向，offset偏移量，len标注间距，arrowsize箭头大小，exline标注延伸线距离，height文本高度，text字符串
        public static void Singlearrow(DxfDocument dxf1, Vector2 hp1, double ang1, double offset, double ang2, double len, double arrowsize, double exline, double height, double jj, String text, String toh, String tol)  //单箭头

        {
            var v0 = hp1;
            var v1 = fun.Polar(v0, ang1 * Math.PI / 180, offset);
            var v2 = fun.Polar(v0, ang1 * Math.PI / 180, offset + exline);
            var v3 = fun.Polar(v1, (ang2 + 10) * Math.PI / 180, arrowsize / Math.Cos(10 * Math.PI / 180));
            var v4 = fun.Polar(v1, (ang2 - 10) * Math.PI / 180, arrowsize / Math.Cos(10 * Math.PI / 180));
            var v5 = fun.Polar(v1, ang2 * Math.PI / 180, len);

            Vector2 v6p;
            if (ang2 >= 0 && ang2 <= 180)
            {
                v6p = fun.Polar(v1, ang2 * Math.PI / 180, len / 2.5);
            }
            else
            {
                v6p = fun.Polar(v1, ang2 * Math.PI / 180, len / 2);
            }

            TextStyle style = new TextStyle("E:\\ikun\\simsun.ttf");
            int l = text.Length;
            int l1 = toh.Length;
            int l2 = tol.Length;
            var pi = Math.PI;
            var L = l * height * 0.7;

            var line1 = new Line(v0, v2);
            var line2 = new Line(v1, v5);

            Solid solid1 = new Solid(v1, v4, v3)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };

            double h;
            if (l1 >= l2)
            {
                h = l * height * 0.7 + l1 * height * 0.5 * 0.7;
            }
            else
            {
                h = l * height * 0.7 + l2 * height * 0.5 * 0.7;
            }
            var height2 = height / 2;
            var v6pp = fun.Polar(v6p, ang2 * Math.PI / 180, L * 0.8);
            var v6 = fun.Polar(v6pp, ang1 * pi / 180, 0.1 * height);
            var v7 = fun.Polar(v6, -ang2 * pi / 180, L + jj);
            var v8 = fun.Polar(v7, ang1 * pi / 180, height2 * 1.5);
            if (ang2 >= 0 && ang2 <= 180)
            {
                Text text1 = new Text(text, v6, height, ang2);
                Text text2 = new Text(tol, v7, height2, ang2);
                Text text3 = new Text(toh, v8, height2, ang2);
                dxf1.AddEntity(text1);
                dxf1.AddEntity(text2);
                dxf1.AddEntity(text3);
            }
            else
            {
                Text text1 = new Text(text, v6, height, ang2 + 180);
                Text text2 = new Text(tol, v7, height2, ang2 + 180);
                Text text3 = new Text(toh, v8, height2, ang2 + 180);
                dxf1.AddEntity(text1);
                dxf1.AddEntity(text2);
                dxf1.AddEntity(text3);
            }

            dxf1.AddEntity(line1);
            dxf1.AddEntity(line2);
            dxf1.AddEntity(solid1);
        }


        public static void jz(DxfDocument dxf1, Vector2 hp1, double ang1, double len, String zh)  //基准
        {
            double pi = Math.PI;
            Vector2 hp2 = fun.Polar(hp1, -(90 - ang1) * pi / 180, 4.5 / 2);
            Vector2 hp3 = fun.Polar(hp2, ang1 * pi / 180, 0.3);
            Vector2 hp4 = fun.Polar(hp1, (90 + ang1) * pi / 180, 4.5 / 2);
            Vector2 hp5 = fun.Polar(hp4, ang1 * pi / 180, 0.3);
            Vector2 hp6 = fun.Polar(hp1, ang1 * pi / 180, len);
            Vector2 hp7 = fun.Polar(hp6, ang1 * pi / 180, 3.5);
            Vector2 hp7p = new Vector2(hp7.X - 1.2, hp7.Y - 1.8);


            Solid sd = new Solid(hp2, hp3, hp4, hp5);
            dxf1.AddEntity(sd);
            Line line1 = new Line(hp1, hp6);
            Circle cir = new Circle(hp7, 3.5);
            TextStyle style = new TextStyle("simsun.ttf");
            fun.text(dxf1, hp7p, 3.5, zh);
            dxf1.AddEntity(new EntityObject[] { line1, cir });
        }
        public static void jt0(DxfDocument dxf1, Vector2 hp1, double ang1, double len, double len2, String zh)//(箭头）
        {

            double pi = Math.PI;
            double ang = Math.Atan2(1.0, 6) * 180 / pi;
            double arrowsize = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (ang + ang1) * pi / 180, arrowsize);
            Vector2 a3 = fun.Polar(a1, (ang1 - ang) * pi / 180, arrowsize);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, arrowsize);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, len);

            Solid solid = new Solid(a1, a2, a3);

            Line line1 = new Line(a4, a5);

            fun.text(dxf1, fun.Polar(a5, ang1 * pi / 180, len2), len2, zh);


            dxf1.AddEntity(new EntityObject[] { line1 });
        }
        public static void jt(DxfDocument dxf1, Vector2 hp1, double ang1, double len, double len2, String zh)//(箭头）
        {

            double pi = Math.PI;
            double ang = Math.Atan2(1.0, 6) * 180 / pi;
            double arrowsize = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (ang + ang1) * pi / 180, arrowsize);
            Vector2 a3 = fun.Polar(a1, (ang1 - ang) * pi / 180, arrowsize);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, arrowsize);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, len);

            Solid solid = new Solid(a1, a2, a3);

            Line line1 = new Line(a4, a5);

            fun.text(dxf1, fun.Polar(a5, ang1 * pi / 180, len2), len2, zh);


            dxf1.AddEntity(new EntityObject[] { solid, line1 });
        }

        public static void jt1(DxfDocument dxf1, Vector2 hp1, double ang1, double len, double len2, String zh, double height)//(箭头）
        {
            double pi = Math.PI;
            double arrowsize = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (10 + ang1) * pi / 180, arrowsize / Math.Cos(10 * pi / 180));
            Vector2 a3 = fun.Polar(a1, (ang1 - 10) * pi / 180, arrowsize / Math.Cos(10 * pi / 180));
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, 1);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, len);

            Solid solid = new Solid(a1, a2, a3);

            Line line1 = new Line(a4, a5);

            fun.text(dxf1, fun.Polar(a5, ang1 * pi / 180, len2), height, zh);


            dxf1.AddEntity(new EntityObject[] { solid, line1 });
        }

        public static void jtbz(DxfDocument dxf1, Vector2 hp1, double ang1, double len, double len2, String zh, double arrowsize, double height)//锥度
        {
            double pi = 3.1415926535897;
            int a = zh.Length;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (10 + ang1) * pi / 180, arrowsize);
            Vector2 a3 = fun.Polar(a1, (ang1 - 10) * pi / 180, arrowsize);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, arrowsize);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, len);

            Vector2 a6 = fun.Polar(a5, pi, a * height + arrowsize);
            Vector2 a7 = fun.Polar(fun.Polar(a6, 0, len / 10), 10 * pi / 180, arrowsize);
            Vector2 a8 = new Vector2(a7.X, a7.Y - (a7.Y - a5.Y) * 2);

            Solid solid = new Solid(a1, a2, a3);

            Line line1 = new Line(a4, a5);
            Line line2 = new Line(fun.Polar(a6, 0, len / 6), a7);
            Line line3 = new Line(a7, a8);
            Line line4 = new Line(a5, a6);
            Line line5 = new Line(fun.Polar(a6, 0, len / 6), a8);


            fun.text(dxf1, new Vector2(a5.X - a * height * 0.7, a5.Y), height, zh);

            dxf1.AddEntity(line1);
            dxf1.AddEntity(line2);
            dxf1.AddEntity(line3);
            dxf1.AddEntity(line4);
            dxf1.AddEntity(line5);
            dxf1.AddEntity(solid);
        }
        public static void aray1(DxfDocument dxf, LwPolyline lwpolyline, double ang1, double len, double g, double width, netDxf.Tables.Layer layercolor)
        {
            double pi = Math.PI;
            for (int i = 0; i <= g - 2; i++)
            {
                var lwpolyline1 = lwpolyline.Clone() as LwPolyline;
                Vector2 p0 = new Vector2(0, 0);
                Vector2 p1 = fun.Polar(p0, ang1 * pi / 180, len);
                var polyline1 = Pus.OffsetLwPolyline(lwpolyline1, p1);
                fun.PolyFill(dxf, polyline1, width, layercolor);
                lwpolyline = polyline1;
            }
        }




        public static void text(DxfDocument dxf1, Vector2 hp1, double g, String zh)
        {
            TextStyle style = new TextStyle("E:\\ikun\\simsun.ttf");
            Text text = new Text(zh, hp1, g, 0, style);
            //text.WidthFactor = 0.7;
            //text.Layer = new Layer("text");
            text.Layer.Color.Index = 4;
            //            text.Alignment = TextAlignment.TopCenter;
            dxf1.AddEntity(new EntityObject[] { /*line1, cir,*/ text });

        }

        public static void mtext(DxfDocument dxf1, Vector2 hp1, double g, String zh)
        {
            TextStyle style = new TextStyle("E:\\ikun\\simsun.ttf");
            MText text = new MText(zh, hp1, g, 0, style);
            //text.WidthFactor = 0.7;
            //text.Layer = new Layer("text");
            text.Layer.Color.Index = 4;
            //            text.Alignment = TextAlignment.TopCenter;
            dxf1.AddEntity(new EntityObject[] { /*line1, cir,*/ text });

        }
        public static string obtainMString(string str, int size)
        {
            string str1 = "";

            int row = str.Length / size;

            for (int i = 0; i < row - 1; i++)
            {
                int max = size * (i + 1);
                if (max > str.Length)
                {
                    str1 += str.Substring(size * i);
                }
                else
                {
                    str1 += str.Substring(size * i, max);
                }

                str1 += "\\N";
            }
            return str1;
        }


        public static void xwgcbz(DxfDocument dxf1, Vector2 hp1, double l1, double l2, double ang1, double ang2, string gcfh, string gcz, string jz)  //形位公差标注  gcfh,gcz,jz公差符号，公差值，基准
        {
            double pi = Math.PI;
            double c = 3.5;
            double l = gcz.Length;
            double jtjd = Math.Atan2(1.0, 6) * 180 / pi;//箭头角度
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (jtjd + ang1) * pi / 180, c / Math.Cos(jtjd * pi / 180));
            Vector2 a3 = fun.Polar(a1, (ang1 - jtjd) * pi / 180, c / Math.Cos(jtjd * pi / 180));
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, c);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, l1);
            Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, l2);
            Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
            Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
            Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
            Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
            Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, l * c);
            Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, l * c);
            Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
            Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

            Solid solid = new Solid(a1, a2, a3)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };

            Line line1 = new Line(a4, a5);

            Line line2 = new Line(a5, a6);


            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a10));
            line3.Vertexes.Add(new LwPolylineVertex(a7));

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a8));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a9));

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a11));
            line5.Vertexes.Add(new LwPolylineVertex(a13));
            line5.Vertexes.Add(new LwPolylineVertex(a14));
            line5.Vertexes.Add(new LwPolylineVertex(a12));

            dxf1.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line5 });

            fun.xwgcfh(dxf1, fun.Polar(a10, 45 * pi / 180, 4.95), gcfh);
            fun.text(dxf1, fun.Polar(a9, 55 * pi / 180, 2.32), 3.5, gcz);
            fun.text(dxf1, fun.Polar(a12, 37 * pi / 180, 3.18), 3.5, jz);



        }


        //hp1为放置点
        //length1为第一条指引线长度
        //angle1为第一条线角度
        //length2为第二指引条线长度
        //angle2为第二条线角度
        //weighth为框格宽度
        //arrowscale为箭头大小
        //scale为框的尺寸比例
        //text1为从右往左数第一个框内字符
        //text2为中间框字符
        //height为框内字符高
        //fh为公差符号
        //fhscale为公差符号尺寸
        public static void gcbz(DxfDocument dxf, Vector2 hp1, double lenghth1, double angle1, double lenghth2, double angle2, double weighth, double arrowscale, double scale, string text1, string text2, double height, string fh, double fhscale)
        {
            double pi = Math.PI;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + angle1) * pi / 180, 2.05 * arrowscale);
            Vector2 a3 = fun.Polar(a1, (angle1 - 14) * pi / 180, 2.05 * arrowscale);
            Vector2 a4 = fun.Polar(a1, angle1 * pi / 180, 1.5 * arrowscale);
            Vector2 a5 = fun.Polar(a4, angle1 * pi / 180, lenghth1);
            Vector2 a6 = fun.Polar(a5, angle2 * pi / 180, lenghth2);

            //框
            Vector2 a7 = fun.Polar(a6, pi / 2, 2.5 * scale);
            Vector2 a8 = mirrorpoint(a7, a5, a6);
            Vector2 a7_1_1 = fun.Polar(a7, angle2 * pi / 180, weighth);
            Vector2 a7_1_2 = fun.Polar(a7_1_1, angle2 * pi / 180, weighth * 1.5);
            Vector2 a7_1 = fun.Polar(a7_1_2, angle2 * pi / 180, weighth);
            Vector2 a8_1_1 = fun.Polar(a8, angle2 * pi / 180, weighth);
            Vector2 a8_1_2 = fun.Polar(a8_1_1, angle2 * pi / 180, weighth * 1.5);
            Vector2 a8_1 = fun.Polar(a8_1_2, angle2 * pi / 180, weighth);

            Vector2 zi = fun.Inters(a7, a8_1_1, a7_1_1, a8);
            Vector2 zi2 = fun.Inters(a7_1_2, a8_1_1, a7_1_1, a8_1_2);
            if (angle2 == 0)
            {
                zi = fun.Inters(a7_1_2, a8_1, a7_1, a8_1_2);
            }

            Line line1 = new Line(a4, a5);
            Line line2 = new Line(a5, a6);
            Line line3 = new Line(a6, a7);
            Line line4 = new Line(a8, a7);
            Line line5 = new Line(a7, a7_1);
            Line line6 = new Line(a8_1, a7_1);
            Line line7 = new Line(a8_1, a8);
            Line line8 = new Line(a8_1_1, a7_1_1);
            Line line9 = new Line(a8_1_2, a7_1_2);

            Solid solid = new Solid(a1, a2, a3);

            double ziwidth = text1.Length;
            Text text = new Text(text1, new Vector2(zi.X - ziwidth / 1.4, zi.Y - height / 2.0), height, 0);
            double ziwidth2 = text2.Length;
            double number2 = ziwidth2 / (height * 0.7);
            Text text_1 = new Text(text2, new Vector2(zi2.X - ziwidth2 / 1.4, zi2.Y - height / 2.0), height, 0);
            if (number2 > 4)
            {
                text_1 = new Text(text2, new Vector2(zi2.X - ziwidth2 / 1.9, zi2.Y - height / 2.0), height, 0);
            }
            if (scale < 1)
            {
                text_1 = new Text(text2, new Vector2(zi2.X - ziwidth2 / 2.2, zi2.Y - height / 2.0), height, 0);
            }

            if (angle2 == 180)
            {
                fun.xwgcfh1(dxf, new Vector2(a8_1.X + fun.CalDistance(a8_1, a8_1_2) / 2, a8_1.Y + fun.CalDistance(a7, a8) / 2), fh, fhscale);
            }

            if (angle2 == 0)
            {
                fun.xwgcfh1(dxf, new Vector2(a8.X + fun.CalDistance(a8, a8_1_1) / 2, a8.Y + fun.CalDistance(a7, a8) / 2), fh, fhscale);
            }

            dxf.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line5, line6, line7, line8, line9, text, text_1 });
        }

        public static void gcbz_two(DxfDocument dxf, Vector2 hp1, double lenghth1, double angle1, double lenghth2, double angle2, double weighth, double arrowscale, double scale, string text1, double height, string fh, double fhscale)
        {
            double pi = Math.PI;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + angle1) * pi / 180, 2.05 * arrowscale);
            Vector2 a3 = fun.Polar(a1, (angle1 - 14) * pi / 180, 2.05 * arrowscale);
            Vector2 a4 = fun.Polar(a1, angle1 * pi / 180, 1.5 * arrowscale);
            Vector2 a5 = fun.Polar(a4, angle1 * pi / 180, lenghth1);
            Vector2 a6 = fun.Polar(a5, angle2 * pi / 180, lenghth2);

            //框
            Vector2 a7 = fun.Polar(a6, pi / 2, 2.5 * scale);
            Vector2 a8 = mirrorpoint(a7, a5, a6);
            //Vector2 a7_1_1 = fun.Polar(a7, angle2 * pi / 180, weighth);
            Vector2 a7_1_2 = fun.Polar(a7, angle2 * pi / 180, weighth);
            Vector2 a7_1 = fun.Polar(a7_1_2, angle2 * pi / 180, weighth);
            //Vector2 a8_1_1 = fun.Polar(a8, angle2 * pi / 180, weighth);
            Vector2 a8_1_2 = fun.Polar(a8, angle2 * pi / 180, weighth);
            Vector2 a8_1 = fun.Polar(a8_1_2, angle2 * pi / 180, weighth);

            Vector2 zi = fun.Inters(a7_1, a8_1_2, a7_1_2, a8_1);
            //Vector2 zi2 = fun.Inters(a7_1_2, a8_1_1, a7_1_1, a8_1_2);
            /*if (angle2 == 0)
            {
                zi = fun.Inters(a7_1_2, a8_1, a7_1, a8_1_2);
            }*/

            Line line1 = new Line(a4, a5);
            Line line2 = new Line(a5, a6);
            Line line3 = new Line(a6, a7);
            Line line4 = new Line(a8, a7);
            //Line line5 = new Line(a7, a7_1);
            Line line6 = new Line(a8_1, a7_1);
            Line line7 = new Line(a8_1, a8);
            Line line8 = new Line(a8_1_2, a7_1_2);
            Line line9 = new Line(a7, a7_1);

            Solid solid = new Solid(a1, a2, a3);

            double ziwidth = text1.Length;
            Text text = new Text(text1, new Vector2(zi.X - ziwidth / 1.4, zi.Y - height / 2.0), height, 0);

            if (angle2 == 180)
            {
                fun.xwgcfh1(dxf, new Vector2(a8_1.X + fun.CalDistance(a8_1, a8_1_2) / 2, a8_1.Y + fun.CalDistance(a7, a8) / 2), fh, fhscale);
            }

            if (angle2 == 0)
            {
                fun.xwgcfh1(dxf, new Vector2(a8.X + fun.CalDistance(a8, a8_1_2) / 2, a8.Y + fun.CalDistance(a7, a8) / 2), fh, fhscale);
            }

            dxf.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line6, line7, line8, line9, text });
        }

        public static void xwgcfh(DxfDocument dxf1, Vector2 hp1, String zh)  //形位公差符号
        {
            double pi = 3.1415926535897;
            if (zh == "zxd")
            {
                Vector2 v0 = new Vector2(hp1.X, hp1.Y);
                Vector2 v1 = fun.Polar(v0, pi, 2.5);
                Vector2 v2 = fun.Polar(v0, 0, 2.5);
                Line line = new Line(v1, v2);
                dxf1.AddEntity(new EntityObject[] { line });
            }
            if (zh == "pmd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 2);
                Vector2 hp3 = new Vector2(hp1.X + 3, hp1.Y + 2);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 2);
                Vector2 hp5 = new Vector2(hp1.X - 3, hp1.Y - 2);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp5);
                Line line4 = new Line(hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
            }
            if (zh == "jd")
            {
                Vector2 v1 = new Vector2(hp1.X, hp1.Y);
                Vector2 v2 = fun.Polar(v1, -142 * pi / 180, 3.51);
                Vector2 v3 = fun.Polar(v2, 45, 5.9);
                Vector2 v4 = fun.Polar(v2, 0, 4.65);



                Line line1 = new Line(v2, v3);
                Line line2 = new Line(v2, v4);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "yd")
            {
                Circle cir = new Circle(hp1, 2.5);
                dxf1.AddEntity(new EntityObject[] { cir });
            }
            if (zh == "yzd")
            {
                Vector2 v0 = new Vector2(hp1.X, hp1.Y);
                Vector2 v1 = fun.Polar(v0, 160 * pi / 180, 1.5);
                Vector2 v2 = fun.Polar(v1, -110 * pi / 180, 2.3);
                Vector2 v3 = fun.Polar(v1, 70 * pi / 180, 1.8);
                Vector2 v4 = fun.Polar(v0, -20 * pi / 180, 1.5);
                Vector2 v5 = fun.Polar(v4, -110 * pi / 180, 1.8);
                Vector2 v6 = fun.Polar(v4, 70 * pi / 180, 2.3);


                Line line1 = new Line(v2, v3);
                Line line2 = new Line(v6, v5);
                Circle cir = new Circle(v0, 1.5);
                dxf1.AddEntity(new EntityObject[] { line1, line2, cir });
            }
            if (zh == "xlkd")
            {
                Arc arc = new Arc(new Vector2(hp1.X, hp1.Y - 1.5), 3, 0, 180);
                dxf1.AddEntity(new EntityObject[] { arc });
            }
            if (zh == "mlkd")
            {
                Arc arc = new Arc(new Vector2(hp1.X + 0.72, hp1.Y - 1.5), 3, 0, 180);
                Line line1 = new Line(new Vector2(hp1.X - 3 + 0.72, hp1.Y - 1.5), new Vector2(hp1.X + 3 + 0.72, hp1.Y - 1.5));
                dxf1.AddEntity(new EntityObject[] { arc, line1 });
            }
            if (zh == "pxd")
            {
                double ll = 3 / Math.Cos(45 * pi / 180);
                Vector2 hpp = new Vector2(hp1.X - ll * 0.5 * Math.Cos(45 * pi / 180), hp1.Y - ll * 0.5 * Math.Sin(45 * pi / 180));
                Vector2 hppp = new Vector2(hp1.X + ll * 0.5 * Math.Cos(45 * pi / 180), hp1.Y + ll * 0.5 * Math.Sin(45 * pi / 180));
                Vector2 hp2 = new Vector2(hpp.X - 0.2 * ll, hpp.Y);
                Vector2 hp3 = new Vector2(hppp.X - 0.2 * ll, hppp.Y);
                Vector2 hp4 = new Vector2(hpp.X + 0.2 * ll, hpp.Y);
                Vector2 hp5 = new Vector2(hppp.X + 0.2 * ll, hppp.Y);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "czd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 2, hp1.Y - 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 2, hp1.Y - 1.5);
                Vector2 hp4 = new Vector2(hp1.X, hp1.Y + 2.5);
                Vector2 hp5 = new Vector2(hp1.X, hp1.Y - 1.5);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "qxd")
            {
                Vector2 hp2 = new Vector2(hp1.X + 3, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X - 3, hp1.Y - 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 3, hp1.Y - 1.5);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "tzd")
            {
                Circle cir1 = new Circle(hp1, 1.0);
                Circle cir2 = new Circle(hp1, 1.5);

                dxf1.AddEntity(new EntityObject[] { cir1, cir2 });
            }
            if (zh == "dcd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 3, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 3, hp1.Y);
                Vector2 hp4 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp6 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp7 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                Line line3 = new Line(hp6, hp7);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3 });
            }
            if (zh == "wzd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 2, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 2, hp1.Y);
                Vector2 hp4 = new Vector2(hp1.X, hp1.Y + 2);
                Vector2 hp5 = new Vector2(hp1.X, hp1.Y - 2);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                Circle cir = new Circle(hp1, 1.2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, cir });
            }
            if (zh == "ytd")
            {
                Vector2 hp2 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);
                Vector2 hp4 = new Vector2(hp2.X - 2.5 * Math.Sin(55 * pi / 180), hp2.Y - 2.5 * Math.Cos(55 * pi / 180));
                Vector2 hp5 = new Vector2(hp2.X - 2.5 * Math.Sin(35 * pi / 180), hp2.Y - 2.5 * Math.Cos(35 * pi / 180));
                Line line1 = new Line(hp2, hp3);
                Solid sd = new Solid(hp4, hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, sd });
            }
            if (zh == "qtd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);
                Vector2 hp3 = new Vector2(hp2.X - 1.5, hp2.Y);
                Vector2 hp4 = new Vector2(hp2.X + 1.5, hp2.Y);
                Vector2 hp5 = new Vector2(hp3.X + 3, hp3.Y + 3);
                Vector2 hp6 = new Vector2(hp4.X + 3, hp4.Y + 3);
                Vector2 hp7 = new Vector2(hp5.X - 2.5 * Math.Sin(55 * pi / 180), hp5.Y - 2.5 * Math.Cos(55 * pi / 180));
                Vector2 hp8 = new Vector2(hp5.X - 2.5 * Math.Sin(35 * pi / 180), hp5.Y - 2.5 * Math.Cos(35 * pi / 180));
                Vector2 hp9 = new Vector2(hp6.X - 2.5 * Math.Sin(55 * pi / 180), hp6.Y - 2.5 * Math.Cos(55 * pi / 180));
                Vector2 hp10 = new Vector2(hp6.X - 2.5 * Math.Sin(35 * pi / 180), hp6.Y - 2.5 * Math.Cos(35 * pi / 180));
                Line line1 = new Line(hp5, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp6);
                Solid sd1 = new Solid(hp7, hp8, hp5);
                Solid sd2 = new Solid(hp9, hp10, hp6);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, sd1, sd2 });
            }
            if (zh == "zdstzt")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.0, hp1.Y - 1.5);
                Circle cir = new Circle(hp1, 3);
                Text text = new Text("M", hp2, 3.5, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "zxys")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.0, hp1.Y - 1.5);
                Circle cir = new Circle(hp1, 3);
                Text text = new Text("A", hp2, 3.5, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "ysgcd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 0.5, hp1.Y - 1.5);
                Circle cir = new Circle(hp1, 3);
                Text text = new Text("P", hp2, 3.5, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }

            if (zh == "bryz")
            {
                Vector2 hp2 = new Vector2(hp1.X - 0.5, hp1.Y - 1.5);
                Circle cir = new Circle(hp1, 3);
                Text text = new Text("E", hp2, 3.5, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "yx")
            {
                Text text = new Text("(-)", new Vector2(hp1.X - 3.5, hp1.Y - 1.5), 3, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text });
            }
            if (zh == "yxzt")
            {
                Text text = new Text("(+)", new Vector2(hp1.X - 3.5, hp1.Y - 1.5), 3, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text });
            }
            if (zh == "yxjx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X - 2.5, hp1.Y - 1.5);
                Vector2 hp6 = new Vector2(hp1.X + 2.5, hp1.Y - 1.5);

                Text text1 = new Text("(", hp5, 3.5, 0);
                text1.WidthFactor = 0.7;
                Text text2 = new Text(")", hp6, 3.5, 0);
                text2.WidthFactor = 0.7;
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp2, hp4);
                Line line3 = new Line(hp3, hp4);
                dxf1.AddEntity(new EntityObject[] { text1, text2, line1, line2, line3 });
            }
            if (zh == "jzmbwd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp4 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);

                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "jt")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y);
                Vector2 hp4 = new Vector2(hp3.X - 2.5, hp3.Y + 2.5 * Math.Tan(10 * pi / 180));
                Vector2 hp5 = new Vector2(hp3.X - 2.5, hp3.Y - 2.5 * Math.Tan(10 * pi / 180));

                Line line1 = new Line(hp2, hp3);
                Solid sd = new Solid(hp4, hp5, hp3);
                dxf1.AddEntity(new EntityObject[] { line1, sd });
            }
            if (zh == "jzx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y);

                Line line1 = new Line(hp2, hp3);
                dxf1.AddEntity(new EntityObject[] { line1 });
            }
            if (zh == "bcfwwzfx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);

                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp5);
                Line line4 = new Line(hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
            }
        }
        public static void xwgcfh1(DxfDocument dxf1, Vector2 hp1, String zh, double scale)  //形位公差符号
        {
            double pi = 3.1415926535897;
            if (zh == "zxd")
            {
                Vector2 v0 = new Vector2(hp1.X, hp1.Y);
                Vector2 v1 = fun.Polar(v0, pi, 2.5 * scale);
                Vector2 v2 = fun.Polar(v0, 0, 2.5 * scale);
                Line line = new Line(v1, v2);
                dxf1.AddEntity(new EntityObject[] { line });
            }
            if (zh == "pmd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5 * scale, hp1.Y + 2 * scale);
                Vector2 hp3 = new Vector2(hp1.X + 3 * scale, hp1.Y + 2 * scale);
                Vector2 hp4 = new Vector2(hp1.X + 1.5 * scale, hp1.Y - 2 * scale);
                Vector2 hp5 = new Vector2(hp1.X - 3 * scale, hp1.Y - 2 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp5);
                Line line4 = new Line(hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
            }
            if (zh == "jd")
            {
                Vector2 v1 = new Vector2(hp1.X, hp1.Y);
                Vector2 v2 = fun.Polar(v1, -142 * pi / 180, 3.51 * scale);
                Vector2 v3 = fun.Polar(v2, 45, 5.9 * scale);
                Vector2 v4 = fun.Polar(v2, 0, 4.65 * scale);



                Line line1 = new Line(v2, v3);
                Line line2 = new Line(v2, v4);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "yd")
            {
                Circle cir = new Circle(hp1, 2.5 * scale);
                dxf1.AddEntity(new EntityObject[] { cir });
            }
            if (zh == "yzd")
            {
                Vector2 v0 = new Vector2(hp1.X, hp1.Y);
                Vector2 v1 = fun.Polar(v0, 160 * pi / 180, 1.5 * scale);
                Vector2 v2 = fun.Polar(v1, -110 * pi / 180, 2.3 * scale);
                Vector2 v3 = fun.Polar(v1, 70 * pi / 180, 1.8 * scale);
                Vector2 v4 = fun.Polar(v0, -20 * pi / 180, 1.5 * scale);
                Vector2 v5 = fun.Polar(v4, -110 * pi / 180, 1.8 * scale);
                Vector2 v6 = fun.Polar(v4, 70 * pi / 180, 2.3 * scale);


                Line line1 = new Line(v2, v3);
                Line line2 = new Line(v6, v5);
                Circle cir = new Circle(v0, 1.5);
                dxf1.AddEntity(new EntityObject[] { line1, line2, cir });
            }
            if (zh == "xlkd")
            {
                Arc arc = new Arc(new Vector2(hp1.X, hp1.Y - 1.5 * scale), 3 * scale, 0, 180);
                dxf1.AddEntity(new EntityObject[] { arc });
            }
            if (zh == "mlkd")
            {
                Arc arc = new Arc(new Vector2(hp1.X + 0.72 * scale, hp1.Y - 1.5 * scale), 3 * scale, 0, 180);
                Line line1 = new Line(new Vector2(hp1.X - 3 * scale + 0.72 * scale, hp1.Y - 1.5 * scale), new Vector2(hp1.X + 3 * scale + 0.72 * scale, hp1.Y - 1.5 * scale));
                dxf1.AddEntity(new EntityObject[] { arc, line1 });
            }
            if (zh == "pxd")
            {
                double ll = 3 / Math.Cos(45 * pi / 180);
                Vector2 hpp = new Vector2(hp1.X - ll * 0.5 * scale * Math.Cos(45 * pi / 180), hp1.Y - ll * 0.5 * scale * Math.Sin(45 * pi / 180));
                Vector2 hppp = new Vector2(hp1.X + ll * 0.5 * scale * Math.Cos(45 * pi / 180), hp1.Y + ll * 0.5 * scale * Math.Sin(45 * pi / 180));
                Vector2 hp2 = new Vector2(hpp.X - 0.2 * scale * ll, hpp.Y);
                Vector2 hp3 = new Vector2(hppp.X - 0.2 * scale * ll, hppp.Y);
                Vector2 hp4 = new Vector2(hpp.X + 0.2 * scale * ll, hpp.Y);
                Vector2 hp5 = new Vector2(hppp.X + 0.2 * scale * ll, hppp.Y);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "czd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 2 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp3 = new Vector2(hp1.X + 2 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp4 = new Vector2(hp1.X, hp1.Y + 2.5 * scale);
                Vector2 hp5 = new Vector2(hp1.X, hp1.Y - 1.5 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "qxd")
            {
                Vector2 hp2 = new Vector2(hp1.X + 3 * scale, hp1.Y + 1.5 * scale);
                Vector2 hp3 = new Vector2(hp1.X - 3 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp4 = new Vector2(hp1.X + 3 * scale, hp1.Y - 1.5 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "tzd")
            {
                Circle cir1 = new Circle(hp1, 1.0 * scale);
                Circle cir2 = new Circle(hp1, 1.5 * scale);

                dxf1.AddEntity(new EntityObject[] { cir1, cir2 });
            }
            if (zh == "dcd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 3 * scale, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 3 * scale, hp1.Y);
                Vector2 hp4 = new Vector2(hp1.X - 1.5 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp5 = new Vector2(hp1.X + 1.5 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp6 = new Vector2(hp1.X - 1.5 * scale, hp1.Y + 1.5 * scale);
                Vector2 hp7 = new Vector2(hp1.X + 1.5 * scale, hp1.Y + 1.5 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                Line line3 = new Line(hp6, hp7);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3 });
            }
            if (zh == "wzd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 2 * scale, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 2 * scale, hp1.Y);
                Vector2 hp4 = new Vector2(hp1.X, hp1.Y + 2 * scale);
                Vector2 hp5 = new Vector2(hp1.X, hp1.Y - 2 * scale);
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                Circle cir = new Circle(hp1, 1.2 * scale);
                dxf1.AddEntity(new EntityObject[] { line1, line2, cir });
            }
            if (zh == "ytd")
            {
                Vector2 hp2 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5 * scale);
                Vector2 hp3 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5 * scale);
                Vector2 hp4 = new Vector2(hp2.X - 2.5 * scale * Math.Sin(55 * pi / 180), hp2.Y - 2.5 * scale * Math.Cos(55 * pi / 180));
                Vector2 hp5 = new Vector2(hp2.X - 2.5 * scale * Math.Sin(35 * pi / 180), hp2.Y - 2.5 * scale * Math.Cos(35 * pi / 180));
                Line line1 = new Line(hp2, hp3);
                Solid sd = new Solid(hp4, hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, sd });
            }
            if (zh == "qtd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5 * scale, hp1.Y - 1.5 * scale);
                Vector2 hp3 = new Vector2(hp2.X - 1.5 * scale, hp2.Y);
                Vector2 hp4 = new Vector2(hp2.X + 1.5 * scale, hp2.Y);
                Vector2 hp5 = new Vector2(hp3.X + 3 * scale, hp3.Y + 3 * scale);
                Vector2 hp6 = new Vector2(hp4.X + 3 * scale, hp4.Y + 3 * scale);
                Vector2 hp7 = new Vector2(hp5.X - 2.5 * scale * Math.Sin(55 * pi / 180), hp5.Y - 2.5 * scale * Math.Cos(55 * pi / 180));
                Vector2 hp8 = new Vector2(hp5.X - 2.5 * scale * Math.Sin(35 * pi / 180), hp5.Y - 2.5 * scale * Math.Cos(35 * pi / 180));
                Vector2 hp9 = new Vector2(hp6.X - 2.5 * scale * Math.Sin(55 * pi / 180), hp6.Y - 2.5 * scale * Math.Cos(55 * pi / 180));
                Vector2 hp10 = new Vector2(hp6.X - 2.5 * scale * Math.Sin(35 * pi / 180), hp6.Y - 2.5 * scale * Math.Cos(35 * pi / 180));
                Line line1 = new Line(hp5, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp6);
                Solid sd1 = new Solid(hp7, hp8, hp5);
                Solid sd2 = new Solid(hp9, hp10, hp6);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, sd1, sd2 });
            }
            if (zh == "zdstzt")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.0 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("M", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "zxys")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.0 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("A", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "ysgcd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 0.5 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("P", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }

            if (zh == "bryz")
            {
                Vector2 hp2 = new Vector2(hp1.X - 0.5 * scale, hp1.Y - 1.5 * scale);
                Circle cir = new Circle(hp1, 3 * scale);
                Text text = new Text("E", hp2, 3.5 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { cir, text });
            }
            if (zh == "yx")
            {
                Text text = new Text("(-)", new Vector2(hp1.X - 3.5 * scale, hp1.Y - 1.5 * scale), 3 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text });
            }
            if (zh == "yxzt")
            {
                Text text = new Text("(+)", new Vector2(hp1.X - 3.5 * scale, hp1.Y - 1.5 * scale), 3 * scale, 0);
                text.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text });
            }
            if (zh == "yxjx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X - 2.5, hp1.Y - 1.5);
                Vector2 hp6 = new Vector2(hp1.X + 2.5, hp1.Y - 1.5);

                Text text1 = new Text("(", hp5, 3.5, 0);
                text1.WidthFactor = 0.7;
                Text text2 = new Text(")", hp6, 3.5, 0);
                text2.WidthFactor = 0.7;
                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp2, hp4);
                Line line3 = new Line(hp3, hp4);
                dxf1.AddEntity(new EntityObject[] { text1, text2, line1, line2, line3 });
            }
            if (zh == "jzmbwd")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp4 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);

                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp4, hp5);
                dxf1.AddEntity(new EntityObject[] { line1, line2 });
            }
            if (zh == "jt")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5 * scale, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5 * scale, hp1.Y);
                Vector2 hp4 = new Vector2(hp3.X - 2.5 * scale, hp3.Y + 2.5 * scale * Math.Tan(10 * pi / 180));
                Vector2 hp5 = new Vector2(hp3.X - 2.5 * scale, hp3.Y - 2.5 * scale * Math.Tan(10 * pi / 180));

                Line line1 = new Line(hp2, hp3);
                Solid sd = new Solid(hp4, hp5, hp3);
                dxf1.AddEntity(new EntityObject[] { line1, sd });
            }
            if (zh == "jzx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y);

                Line line1 = new Line(hp2, hp3);
                dxf1.AddEntity(new EntityObject[] { line1 });
            }
            if (zh == "bcfwwzfx")
            {
                Vector2 hp2 = new Vector2(hp1.X - 1.5, hp1.Y + 1.5);
                Vector2 hp3 = new Vector2(hp1.X + 1.5, hp1.Y + 1.5);
                Vector2 hp4 = new Vector2(hp1.X + 1.5, hp1.Y - 1.5);
                Vector2 hp5 = new Vector2(hp1.X - 1.5, hp1.Y - 1.5);

                Line line1 = new Line(hp2, hp3);
                Line line2 = new Line(hp3, hp4);
                Line line3 = new Line(hp4, hp5);
                Line line4 = new Line(hp5, hp2);
                dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
            }
        }

        public static void text1(DxfDocument dxf1, Vector2 hp1, double ang1, String zh)
        {
            TextStyle style = new TextStyle("E:\\ikun\\simsun.ttf");
            Text text = new Text(zh, hp1, 2.5 / 2, 0, style);
            text.Rotation = (ang1 - 90);
            text.Layer.Color.Index = 4;
            dxf1.AddEntity(new EntityObject[] { text });

        }

        public static void gczyx(DxfDocument dxf1, Vector2 hp1, double ang1, double ang2)  //公差指引线
        {
            double pi = 3.1415926535897;
            double c = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + ang1) * pi / 180, 2.05);
            Vector2 a3 = fun.Polar(a1, (ang1 - 14) * pi / 180, 2.05);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, 1.5);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, 40);
            Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, 20);
            Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
            Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
            Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
            Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
            Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, 4 * c);
            Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, 4 * c);
            Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
            Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

            Solid solid = new Solid(a1, a2, a3, a4)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };

            Line line1 = new Line(a4, a5);

            Line line2 = new Line(a5, a6);


            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a10));
            line3.Vertexes.Add(new LwPolylineVertex(a7));

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a8));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a9));

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a11));
            line5.Vertexes.Add(new LwPolylineVertex(a13));
            line5.Vertexes.Add(new LwPolylineVertex(a14));
            line5.Vertexes.Add(new LwPolylineVertex(a12));

            dxf1.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4/*, line5*/ });
        }
        public static void gczyx1(DxfDocument dxf1, Vector2 hp1, double ang1, double ang2)  //公差指引线(圆）
        {
            double pi = 3.1415926535897;
            double c = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + ang1) * pi / 180, 2.05);
            Vector2 a3 = fun.Polar(a1, (ang1 - 14) * pi / 180, 2.05);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, 1.5);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, 40);
            Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, 20);
            Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
            Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
            Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
            Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
            Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, 4 * c);
            Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, 4 * c);
            Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
            Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

            Circle circle = new Circle(a1, 0.5);
            circle.Lineweight = Lineweight.W50;

            Line line1 = new Line(a1, a5);

            Line line2 = new Line(a5, a6);


            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a10));
            line3.Vertexes.Add(new LwPolylineVertex(a7));

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a8));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a9));

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a11));
            line5.Vertexes.Add(new LwPolylineVertex(a13));
            line5.Vertexes.Add(new LwPolylineVertex(a14));
            line5.Vertexes.Add(new LwPolylineVertex(a12));

            dxf1.AddEntity(new EntityObject[] { circle, line1, line2, line3, line4/*, line5*/ });
        }
        public static void gczyx2(DxfDocument dxf1, Vector2 hp1, double ang1, double ang2)  //公差指引线
        {
            double pi = 3.1415926535897;
            double c = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + ang1) * pi / 180, 2.05);
            Vector2 a3 = fun.Polar(a1, (ang1 - 14) * pi / 180, 2.05);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, 1.5);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, 40);
            Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, 20);
            Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
            Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
            Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
            Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
            Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, 4 * c);
            Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, 4 * c);
            Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
            Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

            Solid solid = new Solid(a1, a2, a3, a4)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };

            Line line1 = new Line(a4, a5);

            Line line2 = new Line(a5, a6);


            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a10));
            line3.Vertexes.Add(new LwPolylineVertex(a7));

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a8));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a9));

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a11));
            line5.Vertexes.Add(new LwPolylineVertex(a13));
            line5.Vertexes.Add(new LwPolylineVertex(a14));
            line5.Vertexes.Add(new LwPolylineVertex(a12));

            dxf1.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line5 });
        }
        public static void gczyx3(DxfDocument dxf1, Vector2 hp1, double ang1, double ang2)  //公差指引线(圆）
        {
            double pi = 3.1415926535897;
            double c = 3.5;
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (14 + ang1) * pi / 180, 2.05);
            Vector2 a3 = fun.Polar(a1, (ang1 - 14) * pi / 180, 2.05);
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, 1.5);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, 40);
            Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, 20);
            Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
            Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
            Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
            Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
            Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, 4 * c);
            Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, 4 * c);
            Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
            Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

            Circle circle = new Circle(a1, 0.5);
            circle.Lineweight = Lineweight.W50;

            Line line1 = new Line(a1, a5);

            Line line2 = new Line(a5, a6);


            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a10));
            line3.Vertexes.Add(new LwPolylineVertex(a7));

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a8));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a9));

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a11));
            line5.Vertexes.Add(new LwPolylineVertex(a13));
            line5.Vertexes.Add(new LwPolylineVertex(a14));
            line5.Vertexes.Add(new LwPolylineVertex(a12));

            dxf1.AddEntity(new EntityObject[] { circle, line1, line2, line3, line4, line5 });
        }

        //public static void gczyx0(DxfDocument dxf1, Vector2 hp1, double l1, double l2, double ang1, double ang2)  //公差指引线(完善中)
        //{
        //    double pi = 3.1415926535897;
        //    double c = 3.5;
        //    Vector2 a1 = new Vector2(hp1.X, hp1.Y);
        //    Vector2 a2 = fun.Polar(a1, (14 + ang1) * pi / 180, 2.05);
        //    Vector2 a3 = fun.Polar(a1, (ang1 - 14) * pi / 180, 2.05);
        //    Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, 1.5);
        //    Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, l1);
        //    Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, l2);
        //    Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
        //    Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
        //    Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
        //    Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
        //    Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, 4 * c);
        //    Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, 4 * c);
        //    Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
        //    Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

        //    Solid solid = new Solid(a1, a2, a3, a4)
        //    {
        //        Layer = Layer.Default,
        //        Linetype = Linetype.ByBlock,
        //        Color = AciColor.ByBlock
        //    };

        //    Line line1 = new Line(a4, a5);

        //    Line line2 = new Line(a5, a6);


        //    LwPolyline line3 = new LwPolyline();
        //    line3.Vertexes.Add(new LwPolylineVertex(a7));
        //    line3.Vertexes.Add(new LwPolylineVertex(a8));
        //    line3.Vertexes.Add(new LwPolylineVertex(a9));
        //    line3.Vertexes.Add(new LwPolylineVertex(a10));
        //    line3.Vertexes.Add(new LwPolylineVertex(a7));

        //    LwPolyline line4 = new LwPolyline();
        //    line4.Vertexes.Add(new LwPolylineVertex(a8));
        //    line4.Vertexes.Add(new LwPolylineVertex(a11));
        //    line4.Vertexes.Add(new LwPolylineVertex(a12));
        //    line4.Vertexes.Add(new LwPolylineVertex(a9));

        //    LwPolyline line5 = new LwPolyline();
        //    line5.Vertexes.Add(new LwPolylineVertex(a11));
        //    line5.Vertexes.Add(new LwPolylineVertex(a13));
        //    line5.Vertexes.Add(new LwPolylineVertex(a14));
        //    line5.Vertexes.Add(new LwPolylineVertex(a12));

        //    dxf1.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line5 });

        //    fun.xwgcfh(dxf1, fun.Polar(a10, 45 * pi / 180, 4.95), "tzd");
        //    fun.text(dxf1, fun.Polar(a9, 55 * pi / 180, 2.32), 3, "φ0.1");
        //    fun.text(dxf1, fun.Polar(a12, 37 * pi / 180, 3.18), 3, "A");

        //}

        public static void gczyx0(DxfDocument dxf1, Vector2 hp1, double l1, double l2, double ang1, double ang2, string gcfh, string gcz, string jz)  //公差指引线  gcfh,gcz,jz公差符号，公差值，基准
        {
            double pi = Math.PI;
            double c = 3.5;
            double l = gcz.Length;
            double jtjd = Math.Atan2(1.0, 6) * 180 / pi;//箭头角度
            Vector2 a1 = new Vector2(hp1.X, hp1.Y);
            Vector2 a2 = fun.Polar(a1, (jtjd + ang1) * pi / 180, c / Math.Cos(jtjd * pi / 180));
            Vector2 a3 = fun.Polar(a1, (ang1 - jtjd) * pi / 180, c / Math.Cos(jtjd * pi / 180));
            Vector2 a4 = fun.Polar(a1, ang1 * pi / 180, c);
            Vector2 a5 = fun.Polar(a4, ang1 * pi / 180, l1);
            Vector2 a6 = fun.Polar(a5, ang2 * pi / 180, l2);
            Vector2 a7 = fun.Polar(a6, 90 * pi / 180, c);
            Vector2 a8 = fun.Polar(a7, ang2 * pi / 180, 2 * c);
            Vector2 a9 = fun.Polar(a8, -90 * pi / 180, 2 * c);
            Vector2 a10 = fun.Polar(a6, -90 * pi / 180, c);
            Vector2 a11 = fun.Polar(a8, ang2 * pi / 180, l * c);
            Vector2 a12 = fun.Polar(a9, ang2 * pi / 180, l * c);
            Vector2 a13 = fun.Polar(a11, ang2 * pi / 180, 2 * c);
            Vector2 a14 = fun.Polar(a12, ang2 * pi / 180, 2 * c);

            Solid solid = new Solid(a1, a2, a3)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };

            Line line1 = new Line(a4, a5);

            Line line2 = new Line(a5, a6);


            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(a7));
            line3.Vertexes.Add(new LwPolylineVertex(a8));
            line3.Vertexes.Add(new LwPolylineVertex(a9));
            line3.Vertexes.Add(new LwPolylineVertex(a10));
            line3.Vertexes.Add(new LwPolylineVertex(a7));

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(a8));
            line4.Vertexes.Add(new LwPolylineVertex(a11));
            line4.Vertexes.Add(new LwPolylineVertex(a12));
            line4.Vertexes.Add(new LwPolylineVertex(a9));

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(a11));
            line5.Vertexes.Add(new LwPolylineVertex(a13));
            line5.Vertexes.Add(new LwPolylineVertex(a14));
            line5.Vertexes.Add(new LwPolylineVertex(a12));

            dxf1.AddEntity(new EntityObject[] { solid, line1, line2, line3, line4, line5 });

            fun.xwgcfh(dxf1, fun.Polar(a10, 45 * pi / 180, 4.95), gcfh);
            fun.text(dxf1, fun.Polar(a9, 55 * pi / 180, 2.32), 3.5, gcz);
            fun.text(dxf1, fun.Polar(a12, 37 * pi / 180, 3.18), 3.5, jz);



        }

        public static LwPolyline rotateorigin(double angleR, LwPolyline lwPolyline)//旋转多段线
        {
            var v2s = new List<Vector2>();
            var bulges = new List<double>();
            foreach (var vertex in lwPolyline.Vertexes)
            {
                v2s.Add(new Vector2(vertex.Position.X, vertex.Position.Y));
                bulges.Add(vertex.Bulge);
            }
            var v2Rs = new List<Vector2>();
            Matrix3 matrixR = Matrix3.RotationZ(angleR);
            foreach (var v2 in v2s)
            {
                var v3 = new Vector3(v2.X, v2.Y, 0);
                var v3R = matrixR * v3;
                v2Rs.Add(new Vector2(v3R.X, v3R.Y));
            }

            var rLine = Pus.AddPointsToLwPolyLine(v2Rs);
            for (var i = 0; i < lwPolyline.Vertexes.Count; i++)
            {
                rLine.Vertexes[i].Bulge = bulges[i];
            }

            rLine.Layer = lwPolyline.Layer;

            return rLine;
        }




        public static void czd(DxfDocument dxf1, Vector2 hp1, double ang, String zh)  //粗糙度
        {
            double pi = 3.1415926535897;
            int l = zh.Length;
            Vector2 hp2 = new Vector2(hp1.X + 6 * Math.Cos((ang + 30) * pi / 180) / 2, hp1.Y + 6 * Math.Sin((ang + 30) * pi / 180) / 2);
            Vector2 hp3 = new Vector2(hp1.X + 6 * Math.Cos((ang - 30) * pi / 180) / 2, hp1.Y + 6 * Math.Sin((ang - 30) * pi / 180) / 2);
            Vector2 hp4 = new Vector2(hp1.X + 11.0 * Math.Cos((ang - 30) * pi / 180) / 2, hp1.Y + 11.0 * Math.Sin((ang - 30) * pi / 180) / 2);
            Vector2 hp5 = fun.Polar(hp4, (60 - ang) * pi / 180, 9.2 / 2);
            Vector2 hp5p = new Vector2(hp5.X + 3.5 * Math.Cos((ang - 90) * pi / 180) / 2, hp5.Y + 3.5 * Math.Sin((ang - 90) * pi / 180) / 2);
            Vector2 hp6 = fun.Polar(hp4, (ang - 90) * pi / 180, l * 2 / 2);
            if (ang == 90)
            {
                Vector2 hp7 = fun.Polar(hp4, (0 - ang) * pi / 180, 3.5 / 2);
                fun.text1(dxf1, hp7, ang, zh);
            }
            else
            {
                Vector2 hp7 = fun.Polar(hp4, 0 * pi / 180, 3.5 / 2);
                fun.text1(dxf1, hp7, ang, zh);
            }
            Line line1 = new Line(hp1, hp2);
            Line line2 = new Line(hp2, hp3);
            Line line3 = new Line(hp1, hp4);
            Line line4 = new Line(hp4, hp6);
            TextStyle style = new TextStyle("simsun.ttf");
            double ang1;
            //ang为-180至180
            if ((ang >= 0 && ang <= 180))
            {
                ang1 = ang - 90;
            }
            else
            {
                ang1 = ang + 90;
            }

            Text text = new Text(zh, hp5p, 3.5f, ang1, style);
            text.WidthFactor = 0.7;
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            if ((ang >= 0 && ang <= 180))
            {
                text.Alignment = TextAlignment.BottomRight;
            }
            else
            {
                text.Alignment = TextAlignment.TopLeft;
            }

            dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4 });
        }


        public static void Axis(DxfDocument dxf, Vector2 hp1, double ang1, double ang2, String zh1, String zh2, String zh3, double scale, double height, double vertex)  //坐标(vertex是0的话是顶点朝上一个点，是1的话，顶点朝下一个叉,ang1ang2是角度制)
        {
            double pi = Math.PI;
            var v0 = hp1;
            var v1 = fun.Polar(v0, ang1 * pi / 180, 10 * scale);
            var v2 = fun.Polar(v1, fun.CalAngle(v1, v0) + 17 * pi / 180, 6.12 * scale);
            var v3 = fun.mirrorpoint(v2, v0, v1);
            var v4 = fun.Polar(v0, ang1 * pi / 180, 5.8 * scale);
            var v5 = fun.Polar(v0, ang2 * pi / 180, 10 * scale);
            var v6 = fun.Polar(v5, fun.CalAngle(v5, v0) + 17 * pi / 180, 6.12 * scale);
            var v7 = fun.Polar(v5, fun.CalAngle(v5, v0) - 17 * pi / 180, 6.12 * scale);
            var v8 = fun.Polar(v0, ang2 * pi / 180, 5.8 * scale);
            var v9 = fun.Polar(v0, ang1 * pi / 180, 1.7 * scale);
            var v10 = fun.Polar(v0, ang2 * pi / 180, 1.7 * scale);

            Solid solid1 = new Solid(v1, v2, v3, v4)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };
            Solid solid2 = new Solid(v5, v6, v7, v8)
            {
                Layer = Layer.Default,
                Linetype = Linetype.ByBlock,
                Color = AciColor.ByBlock
            };

            var line1 = new Line(v9, v4);
            var line2 = new Line(v10, v8);

            var circle1 = new Circle(v0, 1.7 * scale);

            dxf.AddEntity(new EntityObject[] { solid1, solid2, line1, line2, circle1 });
            var v11 = fun.Polar(v0, (ang1 - 8) * pi / 180, 11 * scale);
            var v12 = fun.Polar(v0, (ang2 - 5) * pi / 180, 16 * scale);
            var v13 = fun.Polar(v0, fun.CalAngle(v1, v0), 6 * scale);



            if (vertex == 1)
            {
                Vector2 v14 = fun.Polar(v0, pi / 4, (1.7 - 0.2) * scale);
                Vector2 v15 = fun.Polar(v14, pi + pi / 4, (1.7 - 0.2) * 2 * scale);
                Vector2 v16 = fun.Polar(v0, pi - pi / 4, (1.7 - 0.2) * scale);
                Vector2 v17 = fun.Polar(v16, pi / -4, (1.7 - 0.2) * 2 * scale);

                Line line3 = new Line(v14, v15);
                Line line4 = new Line(v16, v17);
                dxf.AddEntity(new EntityObject[] { line3, line4 });
            }
            else
            {

            }

            Text text1 = new Text(zh1, v11, height, 0);
            dxf.AddEntity(text1);

            Text texe2 = new Text(zh2, v12, height, 0);
            dxf.AddEntity(texe2);

            Text texe3 = new Text(zh3, v13, height, 0);
            dxf.AddEntity(texe3);
        }




        public static void czd1(DxfDocument dxf1, Vector2 hp1, double ang, String zh)  //粗糙度
        {
            double pi = 3.1415926535897;
            Vector2 hp2 = new Vector2(hp1.X + 6 * Math.Cos((ang + 30) * pi / 180), hp1.Y + 6 * Math.Sin((ang + 30) * pi / 180));
            Vector2 hp4 = new Vector2(hp1.X + 11.0 * Math.Cos((ang - 30) * pi / 180), hp1.Y + 11.0 * Math.Sin((ang - 30) * pi / 180));
            Vector2 hp5 = new Vector2(hp1.X + 7 * Math.Cos(30 * pi / 180) * Math.Cos(ang * pi / 180), hp1.Y + 7 * Math.Cos(30 * pi / 180) * Math.Sin(ang * pi / 180));
            Vector2 hp5p = new Vector2(hp5.X + 3.5 * Math.Cos((ang - 90) * pi / 180), hp5.Y + 3.5 * Math.Sin((ang - 90) * pi / 180));
            Line line1 = new Line(hp1, hp2);
            //            Line line2 = new Line(hp2, hp3);
            Line line3 = new Line(hp1, hp4);
            TextStyle style = new TextStyle("simsun.ttf");
            double ang1;
            //ang为-180至180
            if ((ang >= 0 && ang <= 180))
            {
                ang1 = ang - 90;
            }
            else
            {
                ang1 = ang + 90;
            }
            Text text = new Text(zh, hp5p, 3.5f, ang1, style);
            text.WidthFactor = 0.7;
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            if ((ang >= 0 && ang <= 180))
            {
                text.Alignment = TextAlignment.BottomRight;
            }
            else
            {
                text.Alignment = TextAlignment.TopLeft;
            }
            dxf1.AddEntity(new EntityObject[] { line1, line3, text });
        }

        public static void czd2(DxfDocument dxf1, Vector2 hp1, double ang, String zh)  //粗糙度
        {
            double pi = 3.1415926535897;
            Vector2 hp2 = new Vector2(hp1.X + 6 * Math.Cos((ang + 30) * pi / 180), hp1.Y + 6 * Math.Sin((ang + 30) * pi / 180));
            Vector2 hp3 = new Vector2(hp1.X + (2.0 / Math.Sin(30 * pi / 180)) * Math.Cos(ang * pi / 180), hp1.Y + (2.0 / Math.Sin(30 * pi / 180)) * Math.Sin(ang * pi / 180));
            Vector2 hp4 = new Vector2(hp1.X + 11.0 * Math.Cos((ang - 30) * pi / 180), hp1.Y + 11.0 * Math.Sin((ang - 30) * pi / 180));
            Vector2 hp5 = new Vector2(hp1.X + 7 * Math.Cos(30 * pi / 180) * Math.Cos(ang * pi / 180), hp1.Y + 7 * Math.Cos(30 * pi / 180) * Math.Sin(ang * pi / 180));
            Vector2 hp5p = new Vector2(hp5.X + 3.5 * Math.Cos((ang - 90) * pi / 180), hp5.Y + 3.5 * Math.Sin((ang - 90) * pi / 180));
            Line line1 = new Line(hp1, hp2);
            Circle cir = new Circle(hp3, 2);
            Line line3 = new Line(hp1, hp4);
            TextStyle style = new TextStyle("simsun.ttf");
            double ang1;
            //ang为-180至180
            if ((ang >= 0 && ang <= 180))
            {
                ang1 = ang - 90;
            }
            else
            {
                ang1 = ang + 90;
            }
            Text text = new Text(zh, hp5p, 3.5f, ang1, style);
            text.WidthFactor = 0.7;
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            if ((ang >= 0 && ang <= 180))
            {
                text.Alignment = TextAlignment.BottomRight;
            }
            else
            {
                text.Alignment = TextAlignment.TopLeft;
            }
            dxf1.AddEntity(new EntityObject[] { line1, cir, line3, text });
        }

        public static void czd3(DxfDocument dxf1, Vector2 hp1, double ang, String zh1, String zh2)  //粗糙度
        {
            double pi = 3.1415926535897;
            Vector2 hp2 = new Vector2(hp1.X + 6 * Math.Cos((ang + 30) * pi / 180), hp1.Y + 6 * Math.Sin((ang + 30) * pi / 180));
            Vector2 hp3 = new Vector2(hp1.X + 6 * Math.Cos((ang - 30) * pi / 180), hp1.Y + 6 * Math.Sin((ang - 30) * pi / 180));
            Vector2 hp4 = new Vector2(hp1.X + 11.0 * Math.Cos((ang - 30) * pi / 180), hp1.Y + 11.0 * Math.Sin((ang - 30) * pi / 180));
            Vector2 hp5 = new Vector2(hp1.X + 7 * Math.Cos(30 * pi / 180) * Math.Cos(ang * pi / 180), hp1.Y + 7 * Math.Cos(30 * pi / 180) * Math.Sin(ang * pi / 180));
            Vector2 hp5p = new Vector2(hp5.X + 3.5 * Math.Cos((ang - 90) * pi / 180), hp5.Y + 3.5 * Math.Sin((ang - 90) * pi / 180));
            Vector2 hp6 = new Vector2(hp1.X + 11 * Math.Cos(30 * pi / 180) * Math.Cos(ang * pi / 180), hp1.Y + 11 * Math.Cos(30 * pi / 180) * Math.Sin(ang * pi / 180));
            Vector2 hp6p = new Vector2(hp6.X + 3.5 * Math.Cos((ang - 90) * pi / 180), hp6.Y + 3.5 * Math.Sin((ang - 90) * pi / 180));
            Line line1 = new Line(hp1, hp2);
            Line line2 = new Line(hp2, hp3);
            Line line3 = new Line(hp1, hp4);
            TextStyle style = new TextStyle("simsun.ttf");
            double ang1;
            //ang为-180至180
            if ((ang >= 0 && ang <= 180))
            {
                ang1 = ang - 90;
            }
            else
            {
                ang1 = ang + 90;
            }
            Text text = new Text(zh1, hp5p, 3.5f, ang1, style);
            text.WidthFactor = 0.7;
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            if ((ang >= 0 && ang <= 180))
            {
                text.Alignment = TextAlignment.BottomRight;
            }
            else
            {
                text.Alignment = TextAlignment.TopLeft;
            }

            Text text1 = new Text(zh2, hp6p, 3.5f, ang1, style);
            text1.WidthFactor = 0.7;
            text1.Layer = new Layer("text");
            text1.Layer.Color.Index = 8;
            if ((ang >= 0 && ang <= 180))
            {
                text1.Alignment = TextAlignment.BottomRight;
            }
            else
            {
                text1.Alignment = TextAlignment.TopLeft;
            }
            dxf1.AddEntity(new EntityObject[] { line1, line2, line3, text, text1 });
        }

        public static Arc DrawArc(Vector2 startpoint, Vector2 centerpoint, Vector2 endpoint)  //三点画圆弧，逆时针(起点、圆心、终点)
        {
            double r = CalDistance(centerpoint, startpoint);
            double ang1 = CalAngle(centerpoint, startpoint);
            double ang2 = CalAngle(centerpoint, endpoint);
            Arc arc = new Arc(centerpoint, r, (ang1 * 180 / Math.PI), (ang2 * 180 / Math.PI));
            return arc;

        }
        /// <summary>
        /// 弧形修剪
        /// </summary>
        /// <param name="dxf"></param>
        /// <param name="arc1"></param>
        /// <param name="dim">直线与弧线两个点输入1是y轴打的那个点，输入0即是y轴小的那个点</param>
        /// <param name="bigorsmall">与弧线相交的的那两个点，1是y轴打的点，反之依然</param>
        /// <param name="points">点的集合</param>
        /// <exception cref="Exception"></exception>
        public static void trim(DxfDocument dxf, Arc arc1, double dim, double bigorsmall, params Vector2[] points)  //
        {
            int a = 0;
            int q = 0;
            foreach (Vector2 j in points)
            {
                q++;
            }
            foreach (Vector2 i in points)
            {
                Vector2 p1 = points[a];
                Vector2 p2 = points[a + 1];
                double x1 = p1.X;
                double x2 = p2.X;
                double x3 = arc1.Center.X;
                double y1 = p1.Y;
                double y2 = p2.Y;
                double y3 = arc1.Center.Y;
                double strang1 = arc1.StartAngle;
                double endang1 = arc1.EndAngle;
                double ang1 = (endang1 - strang1) / 2.0 + strang1;
                double k = (y2 - y1) / (x2 - x1);
                double r = arc1.Radius;//写到这里
                double b = y1 - k * x1;
                double d = (b - y3);
                double o = (Math.Pow(2 * k * d - 2 * x3, 2) - 4 * (1 + k * k) * (x3 * x3 + Math.Pow(d, 2) - r * r));
                if (p1.X != p2.X)
                {
                    if (o < 0)
                    {
                        throw new Exception(arc1 + "与直线不存在交点");
                    }
                    else
                    {
                        double x4 = (-(2 * k * d - 2 * x3) + Math.Sqrt(o)) / (2 * (1 + k * k));
                        double x5 = (-(2 * k * d - 2 * x3) - Math.Sqrt(o)) / (2 * (1 + k * k));
                        double y4 = k * x4 + b;
                        double y5 = k * x5 + b;
                        Vector2 p4 = new Vector2(x4, y4);
                        Vector2 p5 = new Vector2(x5, y5);
                        if (x1 < x2)
                        {
                            if (x1 < x4 && x4 < x2 || x1 < x5 && x5 < x2)
                            {
                                double u = Math.Atan((y4 - y3) / (x4 - x3)) * 180 / Math.PI;
                                if (strang1 < endang1)
                                {
                                    if (strang1 < u && u < endang1 || strang1 < u + 180 && u + 180 < endang1)
                                    {
                                        Vector2 p6;
                                        if (dim == 0)
                                        {
                                            Line line1;
                                            if (y4 < y5)
                                            {
                                                p6 = p4;
                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(11111);
                                            }
                                            else
                                            {
                                                p6 = p5;

                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(22222);
                                            }
                                            dxf.AddEntity(line1);
                                        }
                                        else
                                        {
                                            Line line1;
                                            if (y4 > y5)
                                            {
                                                p6 = p4;
                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(333333);
                                            }
                                            else
                                            {
                                                p6 = p5;

                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(222222);
                                            }
                                            dxf.AddEntity(line1);
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception(arc1 + "不在交点上");
                                    }
                                }
                                else
                                {
                                    throw new Exception(arc1 + "的起始位置大于终止位置，请重新设置");
                                }
                            }
                            else
                            {
                                throw new Exception("线段与" + arc1 + "不存在交点");
                            }
                        }
                        else
                        {
                            if (x2 < x4 && x4 < x1 || x2 < x5 && x5 < x1)
                            {
                                double u = -Math.Atan((y4 - y3) / (x4 - x3)) * 180 / Math.PI;
                                if (strang1 < endang1)
                                {
                                    if (strang1 < u && u < endang1 || strang1 < u + 180 && u + 180 < endang1)
                                    {
                                        Vector2 p6;
                                        if (dim == 0)
                                        {
                                            Line line1;
                                            if (y4 < y5)
                                            {
                                                p6 = p4;
                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(555555);
                                            }
                                            else
                                            {
                                                p6 = p5;

                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(666666);
                                            }
                                            dxf.AddEntity(line1);
                                        }
                                        else
                                        {
                                            Line line1;
                                            if (y4 > y5)
                                            {
                                                p6 = p4;
                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(7777777);
                                            }
                                            else
                                            {
                                                p6 = p5;

                                                if (bigorsmall == 0)
                                                {
                                                    line1 = new Line(p6, p1);
                                                }
                                                else
                                                {
                                                    line1 = new Line(p6, p2);
                                                }
                                                //System.Diagnostics.Debug.WriteLine(88888888);
                                            }
                                            dxf.AddEntity(line1);
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception(arc1 + "不在交点上");
                                    }
                                }
                                else
                                {
                                    throw new Exception(arc1 + "的起始位置大于终止位置，请重新设置");
                                }
                            }
                            else
                            {
                                throw new Exception("线段与" + arc1 + "不存在交点");
                            }
                        }
                    }
                    a++;
                }
                else
                {
                    double x4 = i.X;
                    double x5 = x4;
                    double y4 = y3 + Math.Sqrt(r * r - Math.Pow(x4 - x3, 2));
                    double y5 = y3 - Math.Sqrt(r * r - Math.Pow(x4 - x3, 2));
                    Vector2 p4 = new Vector2(x4, y4);
                    Vector2 p5 = new Vector2(x5, y5);
                    Vector2 p6 = fun.Polar(Pus.ToV2(arc1.Center), strang1 * Math.PI / 180, r);
                    Vector2 p7 = fun.Polar(Pus.ToV2(arc1.Center), endang1 * Math.PI / 180, r);
                    double u = Math.Atan((y4 - y3) / (x4 - x3)) * 180 / Math.PI;
                    if (strang1 < endang1)
                    {
                        if (strang1 < u && u < endang1 || strang1 < u + 180 && u + 180 < endang1)
                        {
                            Vector2 p8;
                            if (dim == 0)
                            {
                                Line line1;
                                if (y4 < y5)
                                {
                                    p8 = p4;
                                    if (bigorsmall == 0)
                                    {
                                        line1 = new Line(p8, p1);
                                    }
                                    else
                                    {
                                        line1 = new Line(p8, p2);
                                    }
                                    //System.Diagnostics.Debug.WriteLine(99999999);
                                }
                                else
                                {
                                    p8 = p5;

                                    if (bigorsmall == 0)
                                    {
                                        line1 = new Line(p8, p1);
                                    }
                                    else
                                    {
                                        line1 = new Line(p8, p2);
                                    }
                                    //System.Diagnostics.Debug.WriteLine(1010101010);
                                }
                                dxf.AddEntity(line1);
                            }
                            else
                            {
                                Line line1;
                                if (y4 > y5)
                                {
                                    p8 = p4;
                                    if (bigorsmall == 0)
                                    {
                                        line1 = new Line(p8, p1);
                                    }
                                    else
                                    {
                                        line1 = new Line(p8, p2);
                                    }
                                    //System.Diagnostics.Debug.WriteLine(11111111111111111111);
                                }
                                else
                                {
                                    p8 = p5;

                                    if (bigorsmall == 0)
                                    {
                                        line1 = new Line(p8, p1);
                                    }
                                    else
                                    {
                                        line1 = new Line(p8, p2);
                                    }
                                    //System.Diagnostics.Debug.WriteLine(12121212121212);
                                }
                                dxf.AddEntity(line1);
                            }
                        }
                        else
                        {
                            throw new Exception(arc1 + "不在交点上");
                        }
                    }
                    else
                    {
                        throw new Exception(arc1 + "的起始位置大于终止位置，请重新设置");
                    }
                    a++;
                }

                if (a < q - 1)
                { }
                else
                { break; }
            }
        }
        public static Arc DrawArc3(Vector2 v1, Vector2 v2, Vector2 v3)  //三点画圆弧（起点，弧上任意一点、终点）
        {

            var a = v1.X - v2.X;
            var b = v1.Y - v2.Y;
            var c = v1.X - v3.X;
            var d = v1.Y - v3.Y;
            var e = ((v1.X * v1.X - v2.X * v2.X) + (v1.Y * v1.Y - v2.Y * v2.Y)) / 2.0;
            var f = ((v1.X * v1.X - v3.X * v3.X) + (v1.Y * v1.Y - v3.Y * v3.Y)) / 2.0;

            var det = b * c - a * d;

            double x0 = -(d * e - b * f) / det;

            double y0 = -(a * f - c * e) / det;
            var v0 = new Vector2(x0, y0);

            double ang1 = CalAngle(v0, v1);
            double ang2 = CalAngle(v0, v1);

            double radius = Math.Sqrt((v1.X - x0) * (v1.X - x0) + (v1.Y - y0) * (v1.Y - y0));



            Arc arc = fun.DrawArc(v1, v0, v3);


            return arc;
        }



        public static double CalDistance(Vector2 firstpoint, Vector2 sencondpoint)  //计算两点距离
        {
            Double x = sencondpoint.X - firstpoint.X;
            Double y = sencondpoint.Y - firstpoint.Y;
            double v = x * x + y * y;
            double sqrt = Math.Sqrt(v);
            if (sqrt < 0)
            {
                sqrt = -sqrt;
            }
            return sqrt;
        }

        public static double CalAngle(Vector2 firstpoint, Vector2 sencondpoint)  //firstpoint指向sencondpoint的向量，计算直线角度
        {
            double x = sencondpoint.X - firstpoint.X;
            double y = sencondpoint.Y - firstpoint.Y;
            if (x == 0 && y > 0)  //y轴正半轴
            {
                return Math.PI / 2;
            }
            else if (x == 0 && y < 0)  //y轴负半轴
            {
                return Math.PI * 270 / 180;
            }
            else if (x > 0 && y == 0)  //x轴正半轴
            {
                return 0;
            }
            else if (x < 0 && y == 0)  //x轴负半轴
            {
                return Math.PI;
            }
            else if (x > 0 && y > 0)  //第一象限
            {
                return Math.Atan(y / x);
            }
            else if (x < 0 && y > 0)  //第二象限
            {
                return Math.PI - Math.Atan(-(y / x));
            }
            else if (x < 0 && y < 0)  //第三象限
            {
                return Math.Atan(y / x) + Math.PI;
            }
            else  //第四象限
            {
                return Math.PI * 2 - Math.Atan(-(y / x));
            }
        }


        public static Vector2 Polar(Vector2 p0, double ang, double dis)  //AutoCAD中的polar命令
        {
            Double x = p0.X + dis * Math.Cos(ang);
            Double y = p0.Y + dis * Math.Sin(ang);
            Vector2 p1 = new Vector2(x, y);

            return p1;

        }
        public static Arc Rotatingcircle(Arc arc1, Vector2 vector2, double ang1)  //圆绕着圆上任意一点旋转
        {
            double rad = arc1.Radius;
            Vector2 p1 = Pus.ToV2(arc1.Center);
            double staang1 = arc1.StartAngle;
            Vector2 p2 = fun.PointRotate(vector2, p1, ang1);
            Vector2 p3 = fun.Polar(p2, staang1 * Math.PI / 180 - ang1, rad);
            Arc arc2 = fun.DrawArc(p3, p2, vector2);
            return arc2;
        }


        public static Vector2 Inters1(Vector2 p1, Vector2 p2, Arc arc, double xz)  //直线与圆弧的交点   xz:两个点二选一1或2；
        {

            var k = (p1.Y - p2.Y) / (p1.X - p2.X);
            var b = p1.Y - k * p1.X;
            var h = arc.Center.X;
            var k1 = arc.Center.Y;
            var r = arc.Radius;

            Vector2 v0;



            double pi = Math.PI;
            double y1;
            double y2;
            double x1;
            double x2;
            Vector2 a1;
            Vector2 a2;
            if (p1.X == p2.X)
            {/*
                var a = 1;
                var b1 = 2 * k1;
                var c = Math.Pow(k1, 2) - Math.Pow(r, 2) + Math.Pow((p1.X - h), 2);
                 y1 = (-b1 + Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c))) / (2 * a);
                 y2 = (-b1 - Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c))) / (2 * a);
                */
                y1 = Math.Sqrt((r * r) - Math.Pow(p1.X - h, 2)) + k1;
                y2 = -Math.Sqrt((r * r) - Math.Pow(p1.X - h, 2)) + k1;
                x1 = p1.X;
                x2 = p1.X;
                System.Diagnostics.Debug.WriteLine(66667);
                System.Diagnostics.Debug.WriteLine(Math.Sqrt((r * r) - Math.Pow(p1.X - h, 2)) + k1);
                System.Diagnostics.Debug.WriteLine(0011111155);
                /* x1 = k1 - Math.Sqrt(Math.Pow(r, 2) - Math.Pow(y1 - h, 2));
                 x2 = k1 + Math.Sqrt(Math.Pow(r, 2) - Math.Pow(y1 - h, 2));*/
                a1 = new Vector2(x1, y1);
                a2 = new Vector2(x2, y2);



                if (xz == 1)
                {
                    v0 = a1;
                }
                else
                {
                    v0 = a2;
                }

            }
            else if (p1.Y == p2.Y)
            {/*
                x1=Math .Sqrt ((r*r)-Math .Pow((p1.Y -k1),2))+h;
                x2=-Math.Sqrt((r * r) - Math.Pow((p1.Y - k1), 2)) + h;

                y1 = p1.Y;
                y2 = p1.Y;
                a1 = new Vector2(x1, y1);
                a2 = new Vector2(x2, y2);
                */
                x1 = Math.Sqrt((r * r) - Math.Pow(p1.Y - k1, 2)) + h;
                x2 = -Math.Sqrt((r * r) - Math.Pow(p1.Y - k1, 2)) + h;
                y1 = p1.Y;
                y2 = p1.Y;
                a1 = new Vector2(x1, y1);
                a2 = new Vector2(x2, y2);
                if (xz == 1)
                {
                    v0 = a1;
                }
                else
                {
                    v0 = a2;
                }
            }


            else
            {
                var a = Math.Pow(k, 2) + 1;
                var b1 = 2 * k * b - 2 * k * k1 - 2 * h;
                var c = Math.Pow(h, 2) + Math.Pow(b, 2) - 2 * b * k1 + Math.Pow(k1, 2) - Math.Pow(r, 2);
                x1 = (-b1 + Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c))) / (2 * a);
                x2 = (-b1 - Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c))) / (2 * a);




                y1 = k * x1 + b;
                y2 = k * x2 + b;

                a1 = new Vector2(x1, y1);
                a2 = new Vector2(x2, y2);
                System.Diagnostics.Debug.WriteLine(a);
                System.Diagnostics.Debug.WriteLine(-Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c)));
                System.Diagnostics.Debug.WriteLine((2 * a));
                System.Diagnostics.Debug.WriteLine(20006);
                if (xz == 1)
                {
                    v0 = a1;
                }
                else
                {
                    v0 = a2;
                }

            }


            /*
            var x1 = (-b1 + Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c))) / 2 * a;
            var x2 = (-b1 - Math.Sqrt((Math.Pow(b1, 2) - 4 * a * c))) / 2 * a;
            var y1 = k * x1 + b;
            var y2 = k * x2 + b;


            var a1 = new Vector2(x1, y1);
            var a2 = new Vector2(x2, y2);            
            var v0 = a1;
            */


            return v0;

        }

        public static Vector2 Inters3(Arc arc1, Arc arc2, double xz)  //两弧线相交，xz1或0选交点；
                                                                      //废弃，不用！！！！转到Inters4
        {
            var pi = Math.PI;
            var x2 = arc2.Center.X;
            var x1 = arc1.Center.X;
            var y2 = arc2.Center.Y;
            var y1 = arc1.Center.Y;
            Vector2 v1 = new Vector2(x1, y1);
            Vector2 v2 = new Vector2(x2, y2);
            var L = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
            var k1 = (y2 - y1) / (x2 - x1);
            var k2 = (-1) / k1;
            var r1 = arc1.Radius;
            var r2 = arc2.Radius;
            var ang1 = CalAngle(v1, v2);
            double AE = (Math.Abs((r1 * r1) - (r2 * r2)) + (L * L) / (2 * L));
            var CE = Math.Sqrt(Math.Abs((r1 * r1) - (AE * AE)));
            Vector2 a1_ = fun.Polar(v1, ang1, AE);
            Vector2 a1 = fun.Polar(a1_, ang1 + 90 * pi / 180, CE);
            Vector2 a2 = fun.Polar(a1_, ang1 - 90 * pi / 180, CE);
            System.Diagnostics.Debug.WriteLine(666670);
            System.Diagnostics.Debug.WriteLine(r1);
            System.Diagnostics.Debug.WriteLine(r2);
            System.Diagnostics.Debug.WriteLine((r1 * r1) - (AE * AE));
            System.Diagnostics.Debug.WriteLine(L);
            System.Diagnostics.Debug.WriteLine(AE);
            System.Diagnostics.Debug.WriteLine(CE);

            System.Diagnostics.Debug.WriteLine(6666670);
            Vector2 v0;
            if (xz == 1)
            {
                v0 = a1;
            }
            else
            {
                v0 = a2;
            }
            return v0;
        }
        public static Vector2 Inters4(Arc arc1, Arc arc2, double xz)  //两弧线相交，xz1或0选交点；
        {
            var pi = Math.PI;
            var x2 = arc2.Center.X;
            var x1 = arc1.Center.X;
            var y2 = arc2.Center.Y;
            var y1 = arc1.Center.Y;
            var r1 = arc1.Radius;
            var r2 = arc2.Radius;
            var R = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
            Vector2 v1 = new Vector2(x1, y1);
            Vector2 v2 = new Vector2(x2, y2);
            double a1 = (x1 + x2) / 2.0;
            double b1 = ((r1 * r1 - r2 * r2) / (2 * R * R)) * (x2 - x1);
            double c1 = 1 / 2.0 * Math.Sqrt(((2 * ((r1 * r1) + (r2 * r2))) / (R * R)) - (Math.Pow((r1 * r1) - (r2 * r2), 2)) / (R * R * R * R) - 1) * (y2 - y1);

            double a2 = (y1 + y2) / 2.0;
            double b2 = ((r1 * r1 - r2 * r2) / (2 * R * R)) * (y2 - y1);
            double c2 = Math.Sqrt(((2 * ((r1 * r1) + (r2 * r2))) / (R * R)) - (Math.Pow((r1 * r1) - (r2 * r2), 2)) / (R * R * R * R) - 1) / 2.0 * (x1 - x2);

            Vector2 z1 = new Vector2(a1 + b1 + c1, a2 + b2 + c2);
            Vector2 z2 = new Vector2(a1 + b1 - c1, a2 + b2 - c2);


            System.Diagnostics.Debug.WriteLine(666670);
            System.Diagnostics.Debug.WriteLine(z1);
            System.Diagnostics.Debug.WriteLine(z2);


            System.Diagnostics.Debug.WriteLine(6666670);
            Vector2 v0;
            if (xz == 1)
            {
                v0 = z1;
            }
            else
            {
                v0 = z2;
            }
            return v0;
        }
        public static Vector2 Inters(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)  //四点求交点
        {
            Param pm1 = Param.CalParam(p1, p2);
            Param pm2 = Param.CalParam(p3, p4);
            return getIntersectPoint(pm1, pm2);
        }

        public static Vector2 getIntersectPoint(Param A, Param B)
        {
            return getIntersectPoint(A.A, A.B, A.C, B.A, B.B, B.C);
        }

        public static Vector2 getIntersectPoint(double a1, double a2, double a3, double b1, double b2, double b3)
        {
            Vector2 p;
            double m = a1 * b2 - b1 * a2;
            if (m == 0)
            {
                return new Vector2(0, 0);
            }
            double x = (b3 * a2 - a3 * b2) / m;
            double y = (a3 * b1 - b3 * a1) / m;
            p = new Vector2(x, y);
            return p;
        }

        public static void gb118207(DxfDocument dxf1, Vector2 hp1, Vector2 hp2, Vector2 hp3, Vector2 hp5, String qz, String zh, String xwgczh1, String xwgczh2, String xwgczh3, String c1)  //
        {
            double pi = 3.1415926535897;
            double h = 3.5;
            double st = zh.Length * h * 0.7;
            double l3 = 0;
            double uu = 0;
            double vv = 0;
            if ((xwgczh3 == "") && (c1 == "")) l3 = 0.0;
            if (c1 != "") l3 = l3 + c1.Length * h * 0.6 + 1.5 * h;
            if (xwgczh3 != "") l3 = l3 + 1.5 * h;
            if (qz == "") uu = 0.5 * h;
            else uu = 0.6 * h;
            if (xwgczh2 == "") vv = 0.3 * h;
            else vv = 2.5 * h;
            double l1 = 2 * h;
            double l2 = uu + st + vv;
            double h1 = l1 + l2 + l3;
            Vector2 hp5p;
            if (hp5.X > hp3.X) hp5p = hp5;
            else hp5p = new Vector2(hp5.X - h1, hp5.Y);
            Vector2 hp6;
            hp6 = new Vector2(hp5p.X, hp5p.Y + h);
            Vector2 hp6p;
            hp6p = new Vector2(hp5p.X, hp5p.Y - h);
            Vector2 hp12;
            hp12 = new Vector2(hp5p.X + h, hp5p.Y);
            Vector2 hp14;
            hp14 = new Vector2(hp5p.X + 3.0 * h + uu, hp5p.Y - 0.5 * h);
            Vector2 hp13;
            hp13 = new Vector2(hp5p.X + 2.6 * h, hp14.Y);
            Vector2 hp15;
            hp15 = new Vector2(hp5p.X + l1 + l2 - h, hp5p.Y);
            Vector2 hp7, hp7p, hp8, hp8p, hp9, hp9p;
            hp7 = new Vector2(hp6.X + l1, hp6.Y);
            hp7p = new Vector2(hp6p.X + l1, hp6p.Y);
            hp8 = new Vector2(hp6.X + l1 + l2, hp6.Y);
            hp8p = new Vector2(hp6p.X + l1 + l2, hp6p.Y);
            hp9 = new Vector2(hp6.X + l1 + l2 + l3, hp6.Y);
            hp9p = new Vector2(hp6p.X + l1 + l2 + l3, hp6p.Y);
            Vector2 hp16;
            hp16 = new Vector2(hp8.X + 0.75 * h, hp14.Y);
            Vector2 hp19;
            hp19 = new Vector2(hp8.X + l3 - 1.3 * h, hp5p.Y);
            double zj1 = Math.Atan(Math.Abs((hp2.Y - hp1.Y) / (hp2.X - hp1.X))) * 180 / pi;
            if ((hp2.X >= hp1.X) && (hp2.Y > hp1.Y)) zj1 = zj1 * 1.0;//第一象限
            if ((hp2.X <= hp1.X) && (hp2.Y > hp1.Y)) zj1 = 180.0 - zj1 * 1.0;//第二象限
            if ((hp2.X <= hp1.X) && (hp2.Y < hp1.Y)) zj1 = 180.0 + zj1 * 1.0;//第三象限
            if ((hp2.X >=
                hp1.X) && (hp2.Y < hp1.Y)) zj1 = 0.0 - zj1 * 1.0;//第四象限


            Line line1 = new Line(hp1, hp2);
            Line line2 = new Line(hp2, hp3);
            Line line3 = new Line(hp3, hp5);

            Line line4 = new Line(hp6, hp6p);
            Line line5 = new Line(hp6p, hp9p);
            Line line6 = new Line(hp9p, hp9);
            Line line7 = new Line(hp9, hp6);
            Line line8 = new Line(hp7, hp7p);
            Line line9 = new Line(hp8, hp8p);
            Text text = new Text(zh, hp14, h, 0);
            text.WidthFactor = 0.7;
            Text text1 = new Text(c1, hp16, h, 0);
            text1.WidthFactor = 0.7;
            Text text2 = new Text(qz, hp13, h, 0);
            text2.WidthFactor = 0.7;
            dxf1.AddEntity(new EntityObject[] { line1, line2, line3, line4, line5, line6, line7, line8, line9, text, text1, text2 });

            xwgcfh(dxf1, hp12, xwgczh1);
            if (xwgczh2 != "") xwgcfh(dxf1, hp15, xwgczh2);
            if (xwgczh3 != "") xwgcfh(dxf1, hp19, xwgczh3);
            Vector2 hp1p = new Vector2(hp1.X + 2.5 / distance1(hp1, hp2) * (hp2.X - hp1.X), hp1.Y + 2.5 / distance1(hp1, hp2) * (hp2.Y - hp1.Y));
            Vector2 hp1pp = new Vector2(hp1p.X + 2.5 * Math.Tan(10 * pi / 180) * Math.Cos((zj1 - 90) * pi / 180), hp1p.Y + 2.5 * Math.Tan(10 * pi / 180) * Math.Sin((zj1 - 90) * pi / 180));
            Vector2 hp1ppp = new Vector2(hp1p.X + 2.5 * Math.Tan(10 * pi / 180) * Math.Cos((zj1 + 90) * pi / 180), hp1p.Y + 2.5 * Math.Tan(10 * pi / 180) * Math.Sin((zj1 + 90) * pi / 180));

            Solid sd = new Solid(hp1pp, hp1ppp, hp1);
            dxf1.AddEntity(new EntityObject[] { sd });


        }


        public static Vector2 mirrorpoint(Vector2 point, Vector2 pp1, Vector2 pp2)  //镜像点
                                                                                    //point为被镜像点，pp1和pp2为镜像参照线的两个端点
        {
            double k, k_angle;
            Vector2 footpoint_p = new Vector2();

            //计算镜向线的斜率以及斜率弧度
            k = (pp2.Y - pp1.Y) / (pp2.X - pp1.X);
            k_angle = Math.Atan((pp2.Y - pp1.Y) / (pp2.X - pp1.X));//斜率弧度
            double pi = Math.PI;

            if (((k * (pp2.Y - pp1.Y)) / (pp2.Y - pp1.Y)) * point.X < point.Y && pp1.X != pp2.X && pp1.Y != pp2.Y)
            {
                //计算点到镜像线的垂足
                Vector2 v1_pointline = fun.Polar(point, -(Math.PI / 2 - k_angle), fun.CalDistance(point, pp1));//点射出的交线
                Vector2 footpoint = fun.Inters(point, v1_pointline, pp1, pp2);//垂足点

                //计算镜像点
                footpoint_p = fun.Polar(footpoint, -(Math.PI / 2 - k_angle), fun.CalDistance(point, footpoint));

                System.Diagnostics.Debug.WriteLine("计算镜像点");
            }


            else if (pp1.X == pp2.X)
            {
                if (pp1.X < point.X)
                {

                    //计算点到镜像线的垂足
                    Vector2 v1_pointline = fun.Polar(point, pi, fun.CalDistance(point, pp1));//点射出的交线
                    Vector2 footpoint = fun.Inters(point, v1_pointline, pp1, pp2);//垂足点

                    //计算镜像点
                    footpoint_p = fun.Polar(footpoint, pi, fun.CalDistance(point, footpoint));

                }

                else
                {
                    //计算点到镜像线的垂足
                    Vector2 v1_pointline = fun.Polar(point, 0, fun.CalDistance(point, pp1));//点射出的交线
                    Vector2 footpoint = fun.Inters(point, v1_pointline, pp1, pp2);//垂足点

                    //计算镜像点
                    footpoint_p = fun.Polar(footpoint, 0, fun.CalDistance(point, footpoint));

                    System.Diagnostics.Debug.WriteLine(2222222222222);//打印

                }

            }

            else if (pp1.Y == pp2.Y)
            {
                if (pp1.Y > point.Y)
                {

                    //计算点到镜像线的垂足
                    Vector2 v1_pointline = fun.Polar(point, pi / 2, fun.CalDistance(point, pp1));//点射出的交线
                    Vector2 footpoint = fun.Inters(point, v1_pointline, pp1, pp2);//垂足点

                    //计算镜像点
                    footpoint_p = fun.Polar(footpoint, pi / 2, fun.CalDistance(point, footpoint));
                }

                else
                {
                    //计算点到镜像线的垂足
                    Vector2 v1_pointline = fun.Polar(point, -pi / 2, fun.CalDistance(point, pp1));//点射出的交线

                    Vector2 footpoint = fun.Inters(point, v1_pointline, new Vector2(pp1.X - fun.CalDistance(point, pp1), pp1.Y), new Vector2(pp2.X + fun.CalDistance(point, pp2), pp2.Y));//垂足点

                    footpoint_p = fun.Polar(footpoint, -pi / 2, fun.CalDistance(point, footpoint));//计算镜像点

                }
            }

            else
            {
                //计算点到镜像线的垂足
                Vector2 v1_pointline = fun.Polar(point, Math.PI - (Math.PI / 2 - k_angle), fun.CalDistance(point, pp1));//点射出的交线
                Vector2 footpoint = fun.Inters(point, v1_pointline, pp1, pp2);//垂足点

                //计算镜像点
                footpoint_p = fun.Polar(footpoint, Math.PI - (Math.PI / 2 - k_angle), fun.CalDistance(point, footpoint));
            }


            return footpoint_p;
        }

        public static void dimrot(DxfDocument dxf1, Vector2 fp1, Vector2 fp2, double ang, double offset, double height, double dax, double jj, String text, String toh, String tol)
        //带有偏差的尺寸标注（fp1为第一个点，fp2为第二个点，ang为标注角度，offset为标注长度，dax为箭头大小，height为text的文本高度，jj为tch以及tol距离text位置，text为标注前面的文本，toh为上偏差，tol为下偏差）

        {
            double pi = 3.1415926535897;
            if (text.Contains("Φ")) text = text + "  ";

            /*            double ang1,angp;
                        if (fp2.X == fp1.X)
                        {
                            if (fp2.Y > fp1.Y) ang1 = 90;
                            else ang1 = -90;
                        }
                        else
                        {
                            ang1=Math.Atan((fp2.Y-fp1.Y)/(fp2.X-fp1.X))*180/pi;
                        }
                        ang1=Math.Abs(ang1);
                        if (fp2.Y > fp1.Y) angp = 90 - ang - (90 - ang1);
                        else angp = 90 + ang - (90 - ang1);*/
            Vector2 fpp = new Vector2((fp1.X + fp2.X) / 2.0, (fp1.Y + fp2.Y) / 2.0);
            double ang1 = 0, ang2 = 0, ang3 = 0;
            if (ang > -90 && ang <= 90)
            {
                ang1 = ang; ang2 = 90 + ang; ang3 = ang2;
            }
            if (ang >= -180 && ang <= -90)
            {
                ang1 = 180 + ang; ang2 = 90 + ang; ang3 = ang2 + 180;
            }
            if (ang > 90 && ang <= 180)
            {
                ang1 = ang - 180; ang2 = ang - 270; ang3 = ang2 + 180;
            }
            Vector2 fp7p = new Vector2(fpp.X + offset * Math.Cos(ang2 * pi / 180), fpp.Y + offset * Math.Sin(ang2 * pi / 180));

            double xx = (fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X - fp1.Y + Math.Tan(ang2 * pi / 180) * fp1.X) / (Math.Tan(ang2 * pi / 180) - Math.Tan(ang1 * pi / 180));
            double yy = Math.Tan(ang1 * pi / 180) * xx + fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X;
            if (ang1 == 90.0 || ang1 == -90.0)
            {
                xx = fp7p.X;
                yy = fp1.Y;
            }
            Vector2 fp3 = new Vector2(xx, yy);

            double xx1 = (fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X - fp2.Y + Math.Tan(ang2 * pi / 180) * fp2.X) / (Math.Tan(ang2 * pi / 180) - Math.Tan(ang1 * pi / 180));
            double yy1 = Math.Tan(ang1 * pi / 180) * xx1 + fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X;
            if (ang1 == 90.0 || ang1 == -90.0)
            {
                xx1 = fp7p.X;
                yy1 = fp2.Y;
            }

            Vector2 fp4 = new Vector2(xx1, yy1);

            double lp3p4 = distance1(fp3, fp4);

            Vector2 fp5 = new Vector2(fp3.X + 2 * Math.Cos(ang2 * pi / 180), fp3.Y + 2 * Math.Sin(ang2 * pi / 180));
            Vector2 fp6 = new Vector2(fp4.X + 2 * Math.Cos(ang2 * pi / 180), fp4.Y + 2 * Math.Sin(ang2 * pi / 180));
            Vector2 fpl, fpr;
            fpl = fp3; fpr = fp4;

            if (ang >= 0 && ang <= 90)
            {
                if (fp3.X < fp4.X || fp3.Y < fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }
            if (ang > -90 && ang < 0)
            {
                if (fp3.X < fp4.X || fp3.Y > fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }
            if (ang > 90 && ang <= 180)
            {
                if (fp3.X < fp4.X || fp3.Y > fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }
            if (ang >= -180 && ang <= -90)
            {
                if (fp3.X < fp4.X || fp3.Y < fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }

            Vector2 fp7pp = new Vector2(fpl.X + dax / distance1(fp3, fp4) * (fpl.X - fpr.X), fpl.Y + dax / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp7 = new Vector2(fp7pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp7pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp8 = new Vector2(fp7pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp7pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp9pp = new Vector2(fpl.X + dax / distance1(fpr, fpl) * (fpr.X - fpl.X), fpl.Y + dax / distance1(fpl, fpr) * (fpr.Y - fpl.Y));
            Vector2 fp9 = new Vector2(fp9pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp9pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp10 = new Vector2(fp9pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp9pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp11pp = new Vector2(fpr.X + dax / distance1(fpl, fpr) * (fpr.X - fpl.X), fpr.Y + dax / distance1(fpl, fpr) * (fpr.Y - fpl.Y));
            Vector2 fp11 = new Vector2(fp11pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp11pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp12 = new Vector2(fp11pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp11pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp13pp = new Vector2(fpr.X + dax / distance1(fpl, fpr) * (fpl.X - fpr.X), fpr.Y + dax / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp13 = new Vector2(fp13pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp13pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp14 = new Vector2(fp13pp.X + dax * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp13pp.Y + dax * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp15 = new Vector2(fpl.X + (text.Length * 3.5 + Math.Max(tol.Length, toh.Length) * 1.5) * 0.7 / distance1(fpl, fpr) * (fpl.X - fpr.X),
                                       fpl.Y + (text.Length * 3.5 + Math.Max(tol.Length, toh.Length) * 1.5) * 0.7 / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp16 = new Vector2(fpr.X + (text.Length * 3.5 + Math.Max(tol.Length, toh.Length) * 1.5) * 0.7 / distance1(fpl, fpr) * (fpr.X - fpl.X),
                                       fpr.Y + (text.Length * 3.5 + Math.Max(tol.Length, toh.Length) * 1.5) * 0.7 / distance1(fpl, fpr) * (fpr.Y - fpl.Y));

            double jl, angtext;
            //           if (ang > -90 && ang <= 90) jl = 2;
            //           else jl = -2;
            jl = 2;
            angtext = ang1;

            double sxl = 0.0;
            if (!text.Contains("(")) sxl = -4;

            double lentexttol = text.Length * 3.5 * 0.7 + Math.Max(tol.Length, toh.Length) * 1.5 * 0.7;
            double lentexttol0 = text.Length * height * 0.7 + Math.Max(tol.Length, toh.Length) * height * 0.4 * 0.7;

            Vector2 fp18 = new Vector2(fp15.X + jl * Math.Cos(ang3 * pi / 180), fp15.Y + jl * Math.Sin(ang3 * pi / 180));
            Vector2 fp21 = new Vector2(fp16.X + jl * Math.Cos(ang3 * pi / 180), fp16.Y + jl * Math.Sin(ang3 * pi / 180));
            Vector2 fp21p = new Vector2(fp21.X - (Math.Max(tol.Length, toh.Length) * 1.5 * 0.7 + sxl) * 0.5 * Math.Cos(ang1 * pi / 180), fp21.Y - (Math.Max(tol.Length, toh.Length) * 1.5 * 0.7 + sxl) * 0.5 * Math.Sin(ang1 * pi / 180));
            Vector2 fp21pp = new Vector2(fp21p.X + (Math.Max(tol.Length, toh.Length) * 0.7 * 1.5 + 4) * 0.5 * Math.Cos(ang1 * pi / 180), fp21p.Y + (Math.Max(tol.Length, toh.Length) * 0.7 * 1.5 + 4) * 0.5 * Math.Sin(ang1 * pi / 180));

            Vector2 fp19 = new Vector2(fp18.X + (lentexttol + 6 + sxl) * 0.5 * Math.Cos(ang1 * pi / 180), fp18.Y + (lentexttol + 6 + sxl) * 0.5 * Math.Sin(ang1 * pi / 180));
            Vector2 fp19p = new Vector2(fp19.X + Math.Max(tol.Length, toh.Length) * 1.5 * 0.7 * Math.Cos(ang1 * pi / 180), fp19.Y + Math.Max(tol.Length, toh.Length) * 1.5 * 0.7 * Math.Sin(ang1 * pi / 180));
            Vector2 fp20 = new Vector2(fp21.X - (lentexttol - 4.5) * Math.Cos(ang1 * pi / 180), fp21.Y - (lentexttol - 4.5) * Math.Sin(ang1 * pi / 180));

            Vector2 fp17 = new Vector2(fp7p.X + jl * Math.Cos(ang3 * pi / 180), fp7p.Y + jl * Math.Sin(ang3 * pi / 180));


            Vector2 fp22 = new Vector2(fp17.X +
                lentexttol0 * 0.5 * 0.5 / distance1(fpl, fpr) * (fpl.X - fpr.X), fp17.Y + lentexttol * 0.5 * 0.5 / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp23 = new Vector2(fp17.X + (lentexttol * 0.5 + 2 + sxl) * 0.5 / distance1(fpl, fpr) * (fpr.X - fpl.X), fp17.Y + (lentexttol * 0.5 + 2 + sxl) * 0.5 / distance1(fpl, fpr) * (fpr.Y - fpl.Y));
            Vector2 fp23p = new Vector2(fp17.X + (lentexttol - 1) * 0.5 / distance1(fpl, fpr) * (fpr.X - fpl.X), fp17.Y + (lentexttol - 1) * 0.5 / distance1(fpl, fpr) * (fpr.Y - fpl.Y));

            Vector2 fp24 = new Vector2(fp19.X + jl * Math.Cos(ang3 * pi / 180), fp19.Y + jl * Math.Sin(ang3 * pi / 180));
            Vector2 fp25 = new Vector2(fp23.X + jl * Math.Cos(ang3 * pi / 180), fp23.Y + jl * Math.Sin(ang3 * pi / 180));
            Vector2 fp26 = new Vector2(fp21p.X + jl * Math.Cos(ang3 * pi / 180), fp21p.Y + jl * Math.Sin(ang3 * pi / 180));

            if (lentexttol0 < lp3p4)
            {
                Text text1 = new Text(text, fp22, height, angtext);
                text1.WidthFactor = 0.7;
                //Text text2 = new Text(tol, fp23, 1, angtext);
                Text text2 = new Text(tol, fun.Polar(fp23, ang, jj), height * 0.4, angtext);
                text2.WidthFactor = 0.7;
                //Text text3 = new Text(toh, fp25, 1, angtext);
                Text text3 = new Text(toh, fun.Polar(fp25, ang, jj), height * 0.4, angtext);
                text3.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text1, text2, text3 });
                if (text.Contains("("))
                {
                    Text text4 = new Text(")", fp23p, height, angtext);
                    text4.WidthFactor = 0.7;
                    dxf1.AddEntity(new EntityObject[] { text4 });
                }

                Solid sd1 = new Solid(fpl, fp9, fp10);
                Solid sd2 = new Solid(fpr, fp13, fp14);

                dxf1.AddEntity(new EntityObject[] { sd1, sd2 });
            }
            else
            {
                if (distance1(fpl, fp3) < 0.0001)
                {
                    Text text1 = new Text(text, fp18, height, angtext);
                    text1.WidthFactor = 0.7;
                    //Text text2 = new Text(tol, fp19, 1, angtext);
                    Text text2 = new Text(tol, fun.Polar(fp19, ang, jj), height * 0.4, angtext);
                    text2.WidthFactor = 0.7;
                    //Text text3 = new Text(toh, fp24, 1, angtext);
                    Text text3 = new Text(toh, fun.Polar(fp24, ang, jj), height * 0.4, angtext);
                    text3.WidthFactor = 0.7;
                    Line linep = new Line(fpl, fp15);
                    Solid sd1 = new Solid(fpl, fp7, fp8);
                    Solid sd2 = new Solid(fpr, fp11, fp12);
                    Line linepp = new Line(fpr, new Vector2((fp16.X + fpr.X) / 2, (fp16.Y + fpr.Y) / 2));
                    dxf1.AddEntity(new EntityObject[] { text1, text2, text3, linep, sd2, sd1, linepp });
                    if (text.Contains("("))
                    {
                        Text text4 = new Text(")", fp19p, height, angtext);
                        text4.WidthFactor = 0.7;
                        dxf1.AddEntity(new EntityObject[] { text4 });
                    }

                }
                else
                {
                    Text text1 = new Text(text, fp20, height, angtext);
                    text1.WidthFactor = 0.7;
                    //Text text2 = new Text(tol, fp21p, 1, angtext);
                    Text text2 = new Text(tol, fp21p, height * 0.4, angtext);
                    text2.WidthFactor = 0.7;
                    //Text text3 = new Text(toh, fp26, 1, angtext);
                    Text text3 = new Text(toh, fp26, height * 0.4, angtext);
                    text3.WidthFactor = 0.7;
                    Solid sd1 = new Solid(fpl, fp7, fp8);
                    Solid sd2 = new Solid(fpr, fp11, fp12);
                    Line linep = new Line(fpr, fp16);
                    Line linepp = new Line(fpl, new Vector2((fp15.X + fpl.X) / 2, (fp15.Y + fpl.Y) / 2));
                    dxf1.AddEntity(new EntityObject[] { text1, text2, text3, linep, linepp, sd1, sd2 });
                    if (text.Contains("("))
                    {
                        Text text4 = new Text(")", fp21pp, 2.5, angtext);
                        text4.WidthFactor = 0.7;
                        dxf1.AddEntity(new EntityObject[] { text4 });
                    }

                }
            }
            Line line1 = new Line(fp1, fp5);
            Line line2 = new Line(fp2, fp6);
            Line line3 = new Line(fp3, fp4);
            dxf1.AddEntity(new EntityObject[] { line1, line2, line3 });


        }

        public static void dimrot1(DxfDocument dxf1, Vector2 fp1, Vector2 fp2, double ang, double offset, double jj, double height, double arrowsize1, double exline, String text, String toh, String tol)
        //(两个点，方向，偏移量，文本偏移量，文字高度，箭头大小，延伸长度，文本内容）注意toh必须大于tol
        //上：（终点y坐标-起点y坐标）/2.0，是偏移量的起点
        //下：（终点y坐标-起点y坐标）/2.0+起点y坐标，是偏移量的起点
        {
            var ang0 = ang;
            var height2 = height * 0.5;
            Vector2 fp1p = fp1;
            Vector2 fp2p = fp2;
            if (fp2.Y > fp1.Y) { fp1 = fp2p; fp2 = fp1p; }
            double arrowsize = 3.5;

            double pi = 3.1415926535897;
            //       if (text.Contains("φ")) text = text+"  ";

            /*            double ang1,angp;
                        if (fp2.X == fp1.X)
                        {
                            if (fp2.Y > fp1.Y) ang1 = 90;
                            else ang1 = -90;
                        }
                        else
                        {
                            ang1=Math.Atan((fp2.Y-fp1.Y)/(fp2.X-fp1.X))*180/pi;
                        }
                        ang1=Math.Abs(ang1);
                        if (fp2.Y > fp1.Y) angp = 90 - ang - (90 - ang1);
                        else angp = 90 + ang - (90 - ang1);*/
            Vector2 fpp = new Vector2((fp1.X + fp2.X) / 2.0, (fp1.Y + fp2.Y) / 2.0);
            double ang1 = 0, ang2 = 0, ang3 = 0;
            if (ang > -90 && ang <= 90)
            {
                ang1 = ang; ang2 = 90 + ang; ang3 = ang2;
            }
            if (ang >= -180 && ang <= -90)
            {
                ang1 = 180 + ang; ang2 = 90 + ang; ang3 = ang2 + 180;
            }
            if (ang > 90 && ang <= 180)
            {
                ang1 = ang - 180; ang2 = ang - 270; ang3 = ang2 + 180;
            }
            Vector2 fp7p = new Vector2(fpp.X + offset * Math.Cos(ang2 * pi / 180), fpp.Y + offset * Math.Sin(ang2 * pi / 180));

            double xx = (fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X - fp1.Y + Math.Tan(ang2 * pi / 180) * fp1.X) / (Math.Tan(ang2 * pi / 180) - Math.Tan(ang1 * pi / 180));
            double yy = Math.Tan(ang1 * pi / 180) * xx + fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X;
            if (ang1 == 90.0 || ang1 == -90.0)
            {
                xx = fp7p.X;
                yy = fp1.Y;
            }
            Vector2 fp3 = new Vector2(xx, yy);

            double xx1 = (fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X - fp2.Y + Math.Tan(ang2 * pi / 180) * fp2.X) / (Math.Tan(ang2 * pi / 180) - Math.Tan(ang1 * pi / 180));
            double yy1 = Math.Tan(ang1 * pi / 180) * xx1 + fp7p.Y - Math.Tan(ang1 * pi / 180) * fp7p.X;
            if (ang1 == 90.0 || ang1 == -90.0)
            {
                xx1 = fp7p.X;
                yy1 = fp2.Y;
            }

            Vector2 fp4 = new Vector2(xx1, yy1);

            double lp3p4 = distance1(fp3, fp4);

            Vector2 fp5 = new Vector2(fp3.X + exline * Math.Cos(ang2 * pi / 180), fp3.Y + exline * Math.Sin(ang2 * pi / 180));
            Vector2 fp6 = new Vector2(fp4.X + exline * Math.Cos(ang2 * pi / 180), fp4.Y + exline * Math.Sin(ang2 * pi / 180));
            Vector2 fpl, fpr;
            fpl = fp3; fpr = fp4;

            if (ang >= 0 && ang <= 90)
            {
                if (fp3.X < fp4.X || fp3.Y < fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }
            if (ang > -90 && ang < 0)
            {
                if (fp3.X < fp4.X || fp3.Y > fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }
            if (ang > 90 && ang <= 180)
            {
                if (fp3.X < fp4.X || fp3.Y > fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }
            if (ang >= -180 && ang <= -90)
            {
                if (fp3.X < fp4.X || fp3.Y < fp4.Y)
                {
                    fpl = fp3; fpr = fp4;
                }
                else
                {
                    fpl = fp4; fpr = fp3;
                }
            }

            Vector2 fp7pp = new Vector2(fpl.X + arrowsize / distance1(fp3, fp4) * (fpl.X - fpr.X), fpl.Y + arrowsize / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp7 = new Vector2(fp7pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp7pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp8 = new Vector2(fp7pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp7pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp9pp = new Vector2(fpl.X + arrowsize / distance1(fpr, fpl) * (fpr.X - fpl.X), fpl.Y + arrowsize / distance1(fpl, fpr) * (fpr.Y - fpl.Y));
            Vector2 fp9 = new Vector2(fp9pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp9pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp10 = new Vector2(fp9pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp9pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp11pp = new Vector2(fpr.X + arrowsize / distance1(fpl, fpr) * (fpr.X - fpl.X), fpr.Y + arrowsize / distance1(fpl, fpr) * (fpr.Y - fpl.Y));
            Vector2 fp11 = new Vector2(fp11pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp11pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp12 = new Vector2(fp11pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp11pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp13pp = new Vector2(fpr.X + arrowsize / distance1(fpl, fpr) * (fpl.X - fpr.X), fpr.Y + arrowsize / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp13 = new Vector2(fp13pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos(ang2 * pi / 180), fp13pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin(ang2 * pi / 180));
            Vector2 fp14 = new Vector2(fp13pp.X + arrowsize * Math.Tan(10 * pi / 180) * Math.Cos((ang2 + 180) * pi / 180), fp13pp.Y + arrowsize * Math.Tan(10 * pi / 180) * Math.Sin((ang2 + 180) * pi / 180));

            Vector2 fp15 = new Vector2(fpl.X + (text.Length * height + Math.Max(tol.Length, toh.Length) * height2) * 0.7 / distance1(fpl, fpr) * (fpl.X - fpr.X),
                                       fpl.Y + (text.Length * height + Math.Max(tol.Length, toh.Length) * height2) * 0.7 / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp16 = new Vector2(fpr.X + (text.Length * height + Math.Max(tol.Length, toh.Length) * height2) * 0.7 / distance1(fpl, fpr) * (fpr.X - fpl.X),
                                       fpr.Y + (text.Length * height + Math.Max(tol.Length, toh.Length) * height2) * 0.7 / distance1(fpl, fpr) * (fpr.Y - fpl.Y));

            double jl, j2, angtext;
            //           if (ang > -90 && ang <= 90) jl = 2;
            //           else jl = -2;
            jl = 2;
            angtext = ang1;
            j2 = height * 2 / 3.5;

            double sxl = 0.0;
            if (!text.Contains("(")) sxl = -4;

            double length;
            if (tol.Length >= toh.Length)
            {
                length = tol.Length;
            }
            else
            {
                length = toh.Length;
            }
            double lentexttol = text.Length * height * 0.7 + length * height2 * 0.7;



            Vector2 fp18 = new Vector2(fp15.X + jl * Math.Cos(ang3 * pi / 180), fp15.Y + jl * Math.Sin(ang3 * pi / 180));
            Vector2 fp21 = new Vector2(fp16.X + jl * Math.Cos(ang3 * pi / 180), fp16.Y + jl * Math.Sin(ang3 * pi / 180));
            Vector2 fp21p = new Vector2(fp21.X - (length * height2 * 0.7 + sxl) * 0.5 * Math.Cos(ang1 * pi / 180), fp21.Y - (length * height2 * 0.7 + sxl) * 0.5 * Math.Sin(ang1 * pi / 180));
            Vector2 fp21pp = new Vector2(fp21p.X + (length * 0.7 * height2 + 4) * 0.5 * Math.Cos(ang1 * pi / 180), fp21p.Y + (length * 0.7 * height2 + 4) * 0.5 * Math.Sin(ang1 * pi / 180));

            Vector2 fp19 = new Vector2(fp18.X + (lentexttol + 6 + sxl) * 0.5 * Math.Cos(ang1 * pi / 180), fp18.Y + (lentexttol + 6 + sxl) * 0.5 * Math.Sin(ang1 * pi / 180));
            Vector2 fp19p = new Vector2(fp19.X + length * height2 * 0.7 * Math.Cos(ang1 * pi / 180), fp19.Y + length * height2 * 0.7 * Math.Sin(ang1 * pi / 180));
            Vector2 fp20 = new Vector2(fp21.X - (lentexttol - 4.5) * Math.Cos(ang1 * pi / 180), fp21.Y - (lentexttol - 4.5) * Math.Sin(ang1 * pi / 180));

            Vector2 fp17 = new Vector2(fp7p.X + jl * Math.Cos(ang3 * pi / 180), fp7p.Y + jl * Math.Sin(ang3 * pi / 180));


            Vector2 fp22 = new Vector2(fp17.X +
                lentexttol * 0.5 * 0.5 / distance1(fpl, fpr) * (fpl.X - fpr.X), fp17.Y + lentexttol * 0.5 * 0.5 / distance1(fpl, fpr) * (fpl.Y - fpr.Y));
            Vector2 fp23 = new Vector2(fp17.X + (lentexttol * 0.5 + 2 + sxl) * 0.5 / distance1(fpl, fpr) * (fpr.X - fpl.X), fp17.Y + (lentexttol * 0.5 + 2 + sxl) * 0.5 / distance1(fpl, fpr) * (fpr.Y - fpl.Y));
            Vector2 fp23p = new Vector2(fp17.X + (lentexttol - 1) * 0.5 / distance1(fpl, fpr) * (fpr.X - fpl.X), fp17.Y + (lentexttol - 1) * 0.5 / distance1(fpl, fpr) * (fpr.Y - fpl.Y));

            Vector2 fp24 = new Vector2(fp19.X + jl * Math.Cos(ang3 * pi / 180), fp19.Y + j2 * Math.Sin(ang3 * pi / 180));
            Vector2 fp25 = new Vector2(fp23.X + jl * Math.Cos(ang3 * pi / 180), fp23.Y + j2 * Math.Sin(ang3 * pi / 180));
            Vector2 fp26 = new Vector2(fp21p.X + jl * Math.Cos(ang3 * pi / 180), fp21p.Y + j2 * Math.Sin(ang3 * pi / 180));

            if (lentexttol < lp3p4)
            {
                Text text1 = new Text(text, fp22, height, angtext);
                //                    zhc = "context.fillText(\"" + text + "\"," + fp22.X + "," + fp22.Y + ");";
                //                    sw.WriteLine(zhc);


                text1.WidthFactor = 0.7;
                Text text2 = new Text(tol, fun.Polar(fp23, fun.CalAngle(fp22, fp23), jj), height2, angtext);
                //                    zhc = "context.fillText(\"" + tol + "\"," + fp23.X + "," + fp23.Y + ");";
                //                    sw.WriteLine(zhc);

                text2.WidthFactor = 0.7;
                Text text3 = new Text(toh, fun.Polar(fp25, fun.CalAngle(fp22, fp23), jj), height2, angtext);
                //                    zhc = "context.fillText(\"" + toh + "\"," + fp25.X + "," + fp25.Y + ");";
                //                    sw.WriteLine(zhc);


                text3.WidthFactor = 0.7;
                dxf1.AddEntity(new EntityObject[] { text1, text2, text3 });
                if (text.Contains("("))
                {
                    //Text text4 = new Text(")", fp23p, height, angtext);
                    //                        zhc = "context.fillText(\"" + ")" + "\"," + fp23p.X + "," + fp23p.Y + ");";
                    //                        sw.WriteLine(zhc);


                    //text4.WidthFactor = 0.7;
                    //dxf1.AddEntity(new EntityObject[] { text4 });
                }

                Solid sd1 = new Solid(fpl, fp9, fp10);

                Solid sd2 = new Solid(fpr, fp13, fp14);



                dxf1.AddEntity(new EntityObject[] { sd1, sd2 });
            }
            else
            {
                if (distance1(fpl, fp3) < 0.0001)
                {
                    Text text1 = new Text(text, fp18, height, angtext);
                    //                       zhc = "context.fillText(\"" + text + "\"," + fp18.X + "," + fp18.Y + ");";
                    //                     sw.WriteLine(zhc);


                    text1.WidthFactor = 0.7;
                    Text text2 = new Text(tol, fun.Polar(fp19, fun.CalAngle(fp18, fp19), jj * 0.1), height2, angtext);
                    //                        zhc = "context.fillText(\"" + tol + "\"," + fp19.X + "," + fp19.Y + ");";
                    //                        sw.WriteLine(zhc);


                    text2.WidthFactor = 0.7;
                    Text text3 = new Text(toh, fun.Polar(fp24, fun.CalAngle(fp18, fp19), jj * 0.1), height2, angtext);
                    //                        zhc = "context.fillText(\"" + toh + "\"," + fp24.X + "," + fp24.Y + ");";
                    //                        sw.WriteLine(zhc);


                    text3.WidthFactor = 0.7;
                    Line linep = new Line(fpl, fp15);


                    Solid sd1 = new Solid(fpl, fp7, fp8);


                    Solid sd2 = new Solid(fpr, fp11, fp12);


                    Line linepp = new Line(fpr, new Vector2((fp16.X + fpr.X) / 2, (fp16.Y + fpr.Y) / 2));


                    dxf1.AddEntity(new EntityObject[] { text1, text2, text3, linep, sd1, sd2, linepp });
                    if (text.Contains("("))
                    {
                        //Text text4 = new Text(")", fp19p, height, angtext);


                        //text4.WidthFactor = 0.7;
                        //dxf1.AddEntity(new EntityObject[] { text4 });
                    }

                }
                else
                {
                    Text text1 = new Text(text, fp20, height, angtext);
                    //                       zhc = "context.fillText(\"" + text + "\"," + fp20.X + "," + fp20.Y + ");";
                    //                       sw.WriteLine(zhc);


                    text1.WidthFactor = 0.7;
                    Text text2 = new Text(tol, fun.Polar(fp21p, fun.CalAngle(fp20, fp21p), jj), height2, angtext);
                    //                       zhc = "context.fillText(\"" + tol + "\"," + fp21p.X + "," + fp21p.Y + ");";
                    //                       sw.WriteLine(zhc);


                    text2.WidthFactor = 0.7;
                    Text text3 = new Text(toh, fun.Polar(fp26, fun.CalAngle(fp20, fp21p), jj), height2, angtext);
                    //                        zhc = "context.fillText(\"" + toh + "\"," + fp26.X + "," + fp26.Y + ");";
                    //                        sw.WriteLine(zhc);


                    text3.WidthFactor = 0.7;
                    Solid sd1 = new Solid(fpl, fp7, fp8);


                    Solid sd2 = new Solid(fpr, fp11, fp12);


                    Line linep = new Line(fpr, fp16);


                    Line linepp = new Line(fpl, new Vector2((fp15.X + fpl.X) / 2, (fp15.Y + fpl.Y) / 2));


                    dxf1.AddEntity(new EntityObject[] { text1, text2, text3, linep, linepp, sd1, sd2 });
                    if (text.Contains("("))
                    {
                        // Text text4 = new Text(")", fp21pp, height, angtext);
                        //                            zhc = "context.fillText(\"" + ")" + "\"," + fp21pp.X + "," + fp21pp.Y + ");";
                        //                            sw.WriteLine(zhc);


                        //text4.WidthFactor = 0.7;
                        //dxf1.AddEntity(new EntityObject[] { text4 });
                    }

                }
            }
            Line line1 = new Line(fp1, fp5);


            Line line2 = new Line(fp2, fp6);


            Line line3 = new Line(fp3, fp4);


            dxf1.AddEntity(new EntityObject[] { line1, line2, line3 });


        }


        public static Line mirrorline(Line l1, Vector2 pp1, Vector2 pp2)  //镜像 线段
        {
            double k, xcz, ycz, xsjx, ysjx, xcz1, ycz1, xejx, yejx;
            if (pp2.X != pp1.X)
            {
                //计算镜向线的斜率
                k = (pp2.Y - pp1.Y) / (pp2.X - pp1.X);
                //计算直线起点垂足坐标
                xcz = (k * k * pp1.X + k * (l1.StartPoint.Y - pp1.Y) + l1.StartPoint.X) / (k * k + 1);
                ycz = k * (xcz - pp1.X) + pp1.Y;
                xsjx = l1.StartPoint.X + 2 * (xcz - l1.StartPoint.X);
                ysjx = l1.StartPoint.Y + 2 * (ycz - l1.StartPoint.Y);
                //计算直线终点垂足坐标
                xcz1 = (k * k * pp1.X + k * (l1.EndPoint.Y - pp1.Y) + l1.EndPoint.X) / (k * k + 1);
                ycz1 = k * (xcz1 - pp1.X) + pp1.Y;
            }
            else
            {
                //计算直线起点垂足坐标
                xcz = pp1.X;
                ycz = l1.StartPoint.Y;
                xsjx = l1.StartPoint.X + 2 * (xcz - l1.StartPoint.X);
                ysjx = l1.StartPoint.Y + 2 * (ycz - l1.StartPoint.Y);
                //计算直线终点垂足坐标
                xcz1 = pp1.X;
                ycz1 = l1.EndPoint.Y;

            }
            xejx = l1.EndPoint.X + 2 * (xcz1 - l1.EndPoint.X);
            yejx = l1.EndPoint.Y + 2 * (ycz1 - l1.EndPoint.Y);
            Line l1p = new Line(new Vector2(xsjx, ysjx), new Vector2(xejx, yejx));
            l1p.Layer = l1.Layer;
            return l1p;
        }

        public static Arc mirrorarc(Arc arc1, Vector2 pp1, Vector2 pp2)  //镜像 圆弧
        {
            double k, xccz, yccz, xcjx, ycjx, angjxx, angqs1, angzz1;
            if (pp2.X != pp1.X)
            {
                //计算镜向线的斜率
                k = (pp2.Y - pp1.Y) / (pp2.X - pp1.X);
                //计算圆心点垂足坐标
                xccz = (k * k * pp1.X + k * (arc1.Center.Y - pp1.Y) + arc1.Center.X) / (k * k + 1);
                yccz = k * (xccz - pp1.X) + pp1.Y;
                xcjx = arc1.Center.X + 2 * (xccz - arc1.Center.X);
                ycjx = arc1.Center.Y + 2 * (yccz - arc1.Center.Y);
                //计算镜向后起始及终止角
                angjxx = Math.Atan(k) * 180 / 3.1415926535897;
                angzz1 = 2 * angjxx - arc1.StartAngle;
                angqs1 = 2 * angjxx - arc1.EndAngle;
            }
            else
            {
                //计算直线起点垂足坐标
                xccz = pp1.X;
                yccz = arc1.Center.Y;
                xcjx = arc1.Center.X + 2 * (xccz - arc1.Center.X);
                ycjx = arc1.Center.Y + 2 * (yccz - arc1.Center.Y);
                //计算镜向后起始及终止角
                angjxx = 90;
                angzz1 = 2 * angjxx - arc1.StartAngle;
                angqs1 = 2 * angjxx - arc1.EndAngle;

            }
            Arc arc1p = new Arc(new Vector2(xcjx, ycjx), arc1.Radius, angqs1, angzz1);
            arc1p.Layer = arc1.Layer;
            return arc1p;
        }

        public static Circle mirrorcircle(Circle circle1, Vector2 pp1, Vector2 pp2)  //镜像 圆
        {
            double k, xccz, yccz, xcjx, ycjx;
            if (pp2.X != pp1.X)
            {
                //计算镜向线的斜率
                k = (pp2.Y - pp1.Y) / (pp2.X - pp1.X);
                //计算圆心点垂足坐标
                xccz = (k * k * pp1.X + k * (circle1.Center.Y - pp1.Y) + circle1.Center.X) / (k * k + 1);
                yccz = k * (xccz - pp1.X) + pp1.Y;
                xcjx = circle1.Center.X + 2 * (xccz - circle1.Center.X);
                ycjx = circle1.Center.Y + 2 * (yccz - circle1.Center.Y);
            }
            else
            {
                //计算直线起点垂足坐标
                xccz = pp1.X;
                yccz = circle1.Center.Y;
                xcjx = circle1.Center.X + 2 * (xccz - circle1.Center.X);
                ycjx = circle1.Center.Y + 2 * (yccz - circle1.Center.Y);
            }
            Circle circle1p = new Circle(new Vector2(xcjx, ycjx), circle1.Radius);
            circle1p.Layer = circle1.Layer;
            return circle1p;
        }

        public static Line copyline(Line l1, Vector2 pp1, Vector2 pp2)  //复制 线段
        {
            double xcps, ycps, xcpe, ycpe;
            xcps = l1.StartPoint.X + pp2.X - pp1.X;
            ycps = l1.StartPoint.Y + pp2.Y - pp1.Y;
            xcpe = l1.EndPoint.X + pp2.X - pp1.X;
            ycpe = l1.EndPoint.Y + pp2.Y - pp1.Y;
            Line l1p = new Line(new Vector2(xcps, ycps), new Vector2(xcpe, ycpe));
            l1p.Layer = l1.Layer;
            return l1p;
        }

        public static Arc copyarc(Arc arc1, Vector2 pp1, Vector2 pp2)
        {
            double angqs, angzz, rad, xccp, yccp;
            xccp = arc1.Center.X + pp2.X - pp1.X;
            yccp = arc1.Center.Y + pp2.Y - pp1.Y;
            rad = arc1.Radius;
            angqs = arc1.StartAngle;
            angzz = arc1.EndAngle;
            Arc arc1p = new Arc(new Vector2(xccp, yccp), rad, angqs, angzz);
            arc1p.Layer = arc1.Layer;
            return arc1p;
        }

        public static Circle copycircle(Circle circle1, Vector2 pp1, Vector2 pp2)
        {
            double rad, xccp, yccp;
            xccp = circle1.Center.X + pp2.X - pp1.X;
            yccp = circle1.Center.Y + pp2.Y - pp1.Y;
            rad = circle1.Radius;
            Circle circle1p = new Circle(new Vector2(xccp, yccp), rad);
            circle1p.Layer = circle1.Layer;
            return circle1p;
        }

        public static LwPolyline[] arrayrpolyline(LwPolyline lwPolyline, double x)  //绕圆心阵列
        {
            LwPolyline[] l1p = new LwPolyline[1000];
            int numline = 0;
            for (int i = 0; i < x; i++)
            {
                numline++;
                l1p[numline] = Pus.RotateByOrigin((360 / x) * (i + 1) * Math.PI / 180, lwPolyline);
                l1p[numline].Layer = lwPolyline.Layer;
            }

            return l1p;
        }


        public static Line[] arrayrline(Line l1, int numx, int numy, double distx, double disty)//沿直线阵列
        {
            double[,] xzls, yzls, xzle, yzle;
            xzls = new double[100, 100];
            yzls = new double[100, 100];
            xzle = new double[100, 100];
            yzle = new double[100, 100];
            Line[] l1p;
            l1p = new Line[10000];
            int numline = 0;
            for (int i = 1; i < numx; i++)
                for (int j = 1; j < numy; j++)
                {
                    numline++;
                    xzls[i, j] = l1.StartPoint.X + (i - 1) * distx;
                    yzls[i, j] = l1.StartPoint.Y + (j - 1) * disty;
                    xzle[i, j] = l1.EndPoint.X + (i - 1) * distx;
                    yzle[i, j] = l1.EndPoint.Y + (j - 1) * disty;
                    l1p[numline] = new Line(new Vector2(xzls[i, j], yzls[i, j]), new Vector2(xzle[i, j], yzle[i, j]));
                    l1p[numline].Layer = l1.Layer;
                }
            return l1p;
        }



        public static Arc[] arrayrarc(Arc arc1, int numx, int numy, double distx, double disty)
        {
            double[,] xzlc, yzlc;
            xzlc = new double[100, 100];
            yzlc = new double[100, 100];
            Arc[] arc1p;
            arc1p = new Arc[10000];
            int numarc = 0;
            for (int i = 1; i < numx; i++)
                for (int j = 1; j < numy; j++)
                {
                    numarc++;
                    xzlc[i, j] = arc1.Center.X + (i - 1) * distx;
                    yzlc[i, j] = arc1.Center.Y + (j - 1) * disty;
                    arc1p[numarc] = new Arc(new Vector2(xzlc[i, j], yzlc[i, j]), arc1.Radius, arc1.StartAngle, arc1.EndAngle);
                    arc1p[numarc].Layer = arc1.Layer;
                }
            return arc1p;
        }

        public static Circle[] arrayrcircle(Circle circle1, int numx, int numy, double distx, double disty)
        {
            double[,] xzlc, yzlc;
            xzlc = new double[100, 100];
            yzlc = new double[100, 100];
            Circle[] circle1p;
            circle1p = new Circle[10000];
            int numcircle = 0;
            for (int i = 1; i < numx; i++)
                for (int j = 1; j < numy; j++)
                {
                    numcircle++;
                    xzlc[i, j] = circle1.Center.X + (i - 1) * distx;
                    yzlc[i, j] = circle1.Center.Y + (j - 1) * disty;
                    circle1p[numcircle] = new Circle(new Vector2(xzlc[i, j], yzlc[i, j]), circle1.Radius);
                    circle1p[numcircle].Layer = circle1.Layer;
                }
            return circle1p;
        }

        public static double distance(Vector3 pp1, Vector2 pp2)  //两个三维点的距离
        {
            double dist;
            dist = Math.Sqrt(Math.Pow((pp2.X - pp1.X), 2) + Math.Pow((pp2.Y - pp1.Y), 2));
            return dist;
        }

        public static double distance1(Vector2 pp1, Vector2 pp2)  //两个二维点的距离
        {
            double dist;
            dist = Math.Sqrt(Math.Pow((pp2.X - pp1.X), 2) + Math.Pow((pp2.Y - pp1.Y), 2));
            return dist;
        }

        public static Line[] arraypoline(Line l1, int numpo, Vector2 pcen)
        {
            double[] xzls, yzls, xzle, yzle;
            xzls = new double[100];
            yzls = new double[100];
            xzle = new double[100];
            yzle = new double[100];
            Line[] l1p;
            l1p = new Line[10000];
            int numline = 0;
            double[] angpos, angpoe;
            angpos = new double[100];
            angpoe = new double[100];

            //            double ang = Math.Cos(90*3.1415926535897/180);
            for (int i = 1; i < numpo; i++)
            {
                numline++;
                if (l1.StartPoint.X == pcen.X)
                {
                    if (l1.StartPoint.Y > pcen.Y) angpos[i] = 90 + (i - 1) * 360 / numpo;
                    else angpos[i] = -90 + (i - 1) * 360 / numpo;
                }
                else
                {
                    angpos[i] = Math.Atan((l1.StartPoint.Y - pcen.Y) / (l1.StartPoint.X - pcen.X)) * 180 / 3.1415926535897 + (i - 1) * 360 / numpo;
                }

                if (l1.EndPoint.X == pcen.X)
                {
                    if (l1.EndPoint.Y > pcen.Y) angpoe[i] = 90 + (i - 1) * 360 / numpo;
                    else angpoe[i] = -90 + (i - 1) * 360 / numpo;
                }
                else
                {
                    angpoe[i] = Math.Atan((l1.EndPoint.Y - pcen.Y) / (l1.EndPoint.X - pcen.X)) * 180 / 3.1415926535897 + (i - 1) * 360 / numpo;
                }
                angpos[i] = angpos[i] * 3.1415926535897 / 180;
                angpoe[i] = angpoe[i] * 3.1415926535897 / 180;

                xzls[i] = pcen.X + distance(l1.StartPoint, pcen) * Math.Cos(angpos[i]);
                yzls[i] = pcen.Y + distance(l1.StartPoint, pcen) * Math.Sin(angpos[i]);
                xzle[i] = pcen.X + distance(l1.EndPoint, pcen) * Math.Cos(angpoe[i]);
                yzle[i] = pcen.Y + distance(l1.EndPoint, pcen) * Math.Sin(angpoe[i]);
                l1p[numline] = new Line(new Vector2(xzls[i], yzls[i]), new Vector2(xzle[i], yzle[i]));
                l1p[numline].Layer = l1.Layer;
            }
            return l1p;
        }

        public static Arc[] arraypoarc(Arc arc1, int numpo, Vector2 pcen)
        {
            double[] xzlc, yzlc, angpoc, angqs, angzz;
            xzlc = new double[100];
            yzlc = new double[100];
            angpoc = new double[100];
            angqs = new double[100];
            angzz = new double[100];
            Arc[] arc1p;
            arc1p = new Arc[10000];
            int numarc = 0;

            for (int i = 1; i < numpo; i++)
            {
                numarc++;
                if (arc1.Center.X == pcen.X)
                {
                    if (arc1.Center.Y > pcen.Y) angpoc[i] = 90 + (i - 1) * 360 / numpo;
                    else angpoc[i] = -90 + (i - 1) * 360 / numpo;
                }
                else
                {
                    angpoc[i] = Math.Atan((arc1.Center.Y - pcen.Y) / (arc1.Center.X - pcen.X)) * 180 / 3.1415926535897 + (i - 1) * 360 / numpo;
                }

                angpoc[i] = angpoc[i] * 3.1415926535897 / 180;

                xzlc[i] = pcen.X + distance(arc1.Center, pcen) * Math.Cos(angpoc[i]);
                yzlc[i] = pcen.Y + distance(arc1.Center, pcen) * Math.Sin(angpoc[i]);
                angqs[i] = arc1.StartAngle + (i - 1) * 360 / numpo;
                angzz[i] = arc1.EndAngle + (i - 1) * 360 / numpo;
                arc1p[numarc] = new Arc(new Vector2(xzlc[i], yzlc[i]), arc1.Radius, angqs[i], angzz[i]);
                arc1p[numarc].Layer = arc1.Layer;
            }
            return arc1p;
        }

        public static Circle[] arraypocircle(Circle circle1, int numpo, Vector2 pcen)
        {
            double[] xzlc, yzlc, angpoc;
            xzlc = new double[100];
            yzlc = new double[100];
            angpoc = new double[100];
            Circle[] circle1p;
            circle1p = new Circle[10000];
            int numcircle = 0;

            for (int i = 1; i < numpo; i++)
            {
                numcircle++;
                if (circle1.Center.X == pcen.X)
                {
                    if (circle1.Center.Y > pcen.Y) angpoc[i] = 90 + (i - 1) * 360 / numpo;
                    else angpoc[i] = -90 + (i - 1) * 360 / numpo;
                }
                else
                {
                    angpoc[i] = Math.Atan((circle1.Center.Y - pcen.Y) / (circle1.Center.X - pcen.X)) * 180 / 3.1415926535897 + (i - 1) * 360 / numpo;
                }

                angpoc[i] = angpoc[i] * 3.1415926535897 / 180;

                xzlc[i] = pcen.X + distance(circle1.Center, pcen) * Math.Cos(angpoc[i]);
                yzlc[i] = pcen.Y + distance(circle1.Center, pcen) * Math.Sin(angpoc[i]);
                circle1p[numcircle] = new Circle(new Vector2(xzlc[i], yzlc[i]), circle1.Radius);
                circle1p[numcircle].Layer = circle1.Layer;
            }
            return circle1p;
        }

        public static DimensionStyle CreateDimStyle()
        {
            DimensionStyle myStyle = new DimensionStyle("MyStyle");
            myStyle.DimLineExtend = 0.18;
            myStyle.LengthPrecision = 2;
            myStyle.DimScaleOverall = 1;
            //myStyle.DIMLTEX1 = Linetype.Dot;
            myStyle.DimLineColor = AciColor.Default;
            //myStyle.DIMBLK = DimensionArrowhead.ArchitecturalTick;
            myStyle.DimArrow1 = DimensionArrowhead.Box;
            myStyle.DimArrow2 = DimensionArrowhead.Box;
            //myStyle.DIMSE1 = true;
            //myStyle.DIMSE2 = true;
            myStyle.TextColor = AciColor.Default;

            return myStyle;
        }

        public static void ShowDxfDocumentInformation(DxfDocument dxf)
        {
            Console.WriteLine("FILE VERSION: {0}", dxf.DrawingVariables.AcadVer);
            Console.WriteLine();
            Console.WriteLine("FILE COMMENTS: {0}", dxf.Comments.Count);
            foreach (var o in dxf.Comments)
            {
                Console.WriteLine("\t{0}", o);
            }
            Console.WriteLine();
            Console.WriteLine("FILE TIME:");
            Console.WriteLine("\tdrawing created (UTC): {0}.{1}", dxf.DrawingVariables.TduCreate, dxf.DrawingVariables.TduCreate.Millisecond.ToString("000"));
            Console.WriteLine("\tdrawing last update (UTC): {0}.{1}", dxf.DrawingVariables.TduUpdate, dxf.DrawingVariables.TduUpdate.Millisecond.ToString("000"));
            Console.WriteLine("\tdrawing edition time: {0}", dxf.DrawingVariables.TdinDwg);
            Console.WriteLine();
            Console.WriteLine("APPLICATION REGISTRIES: {0}", dxf.ApplicationRegistries.Count);
            foreach (var o in dxf.ApplicationRegistries)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.ApplicationRegistries.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("LAYERS: {0}", dxf.Layers.Count);
            foreach (var o in dxf.Layers)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Layers.GetReferences(o).Count);
                Debug.Assert(ReferenceEquals(o.Linetype, dxf.Linetypes[o.Linetype.Name]), "Object reference not equal.");
            }
            Console.WriteLine();

            Console.WriteLine("LINE TYPES: {0}", dxf.Linetypes.Count);
            foreach (var o in dxf.Linetypes)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Linetypes.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("TEXT STYLES: {0}", dxf.TextStyles.Count);
            foreach (var o in dxf.TextStyles)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.TextStyles.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("DIMENSION STYLES: {0}", dxf.DimensionStyles.Count);
            foreach (var o in dxf.DimensionStyles)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.DimensionStyles.GetReferences(o.Name).Count);
                Debug.Assert(ReferenceEquals(o.TextStyle, dxf.TextStyles[o.TextStyle.Name]), "Object reference not equal.");
                Debug.Assert(ReferenceEquals(o.DimLineLinetype, dxf.Linetypes[o.DimLineLinetype.Name]), "Object reference not equal.");
                Debug.Assert(ReferenceEquals(o.ExtLine1Linetype, dxf.Linetypes[o.ExtLine1Linetype.Name]), "Object reference not equal.");
                Debug.Assert(ReferenceEquals(o.ExtLine2Linetype, dxf.Linetypes[o.ExtLine2Linetype.Name]), "Object reference not equal.");
                if (o.DimArrow1 != null) Debug.Assert(ReferenceEquals(o.DimArrow1, dxf.Blocks[o.DimArrow1.Name]), "Object reference not equal.");
                if (o.DimArrow2 != null) Debug.Assert(ReferenceEquals(o.DimArrow2, dxf.Blocks[o.DimArrow2.Name]), "Object reference not equal.");
            }
            Console.WriteLine();

            Console.WriteLine("MLINE STYLES: {0}", dxf.MlineStyles.Count);
            foreach (var o in dxf.MlineStyles)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.MlineStyles.GetReferences(o.Name).Count);
                foreach (var e in o.Elements)
                {
                    Debug.Assert(ReferenceEquals(e.Linetype, dxf.Linetypes[e.Linetype.Name]), "Object reference not equal.");
                }
            }
            Console.WriteLine();

            Console.WriteLine("UCSs: {0}", dxf.UCSs.Count);
            foreach (var o in dxf.UCSs)
            {
                Console.WriteLine("\t{0}", o.Name);
            }
            Console.WriteLine();

            Console.WriteLine("BLOCKS: {0}", dxf.Blocks.Count);
            foreach (var o in dxf.Blocks)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Blocks.GetReferences(o.Name).Count);
                Debug.Assert(ReferenceEquals(o.Layer, dxf.Layers[o.Layer.Name]), "Object reference not equal.");

                foreach (var e in o.Entities)
                {
                    Debug.Assert(ReferenceEquals(e.Layer, dxf.Layers[e.Layer.Name]), "Object reference not equal.");
                    Debug.Assert(ReferenceEquals(e.Linetype, dxf.Linetypes[e.Linetype.Name]), "Object reference not equal.");
                    Debug.Assert(ReferenceEquals(e.Owner, dxf.Blocks[o.Name]), "Object reference not equal.");
                    foreach (var x in e.XData.Values)
                    {
                        Debug.Assert(ReferenceEquals(x.ApplicationRegistry, dxf.ApplicationRegistries[x.ApplicationRegistry.Name]), "Object reference not equal.");
                    }

                    Text txt = e as Text;
                    if (txt != null) Debug.Assert(ReferenceEquals(txt.Style, dxf.TextStyles[txt.Style.Name]), "Object reference not equal.");

                    MText mtxt = e as MText;
                    if (mtxt != null) Debug.Assert(ReferenceEquals(mtxt.Style, dxf.TextStyles[mtxt.Style.Name]), "Object reference not equal.");

                    Dimension dim = e as Dimension;
                    if (dim != null)
                    {
                        Debug.Assert(ReferenceEquals(dim.Style, dxf.DimensionStyles[dim.Style.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(dim.Block, dxf.Blocks[dim.Block.Name]), "Object reference not equal.");
                    }

                    MLine mline = e as MLine;
                    if (mline != null) Debug.Assert(ReferenceEquals(mline.Style, dxf.MlineStyles[mline.Style.Name]), "Object reference not equal.");

                    Image img = e as Image;
                    if (img != null) Debug.Assert(ReferenceEquals(img.Definition, dxf.ImageDefinitions[img.Definition.Name]), "Object reference not equal.");

                    Insert ins = e as Insert;
                    if (ins != null)
                    {
                        Debug.Assert(ReferenceEquals(ins.Block, dxf.Blocks[ins.Block.Name]), "Object reference not equal.");
                        foreach (var a in ins.Attributes)
                        {
                            Debug.Assert(ReferenceEquals(a.Layer, dxf.Layers[a.Layer.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Linetype, dxf.Linetypes[a.Linetype.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Style, dxf.TextStyles[a.Style.Name]), "Object reference not equal.");
                        }
                    }
                }

                foreach (var a in o.AttributeDefinitions.Values)
                {
                    Debug.Assert(ReferenceEquals(a.Layer, dxf.Layers[a.Layer.Name]), "Object reference not equal.");
                    Debug.Assert(ReferenceEquals(a.Linetype, dxf.Linetypes[a.Linetype.Name]), "Object reference not equal.");
                    foreach (var x in a.XData.Values)
                    {
                        Debug.Assert(ReferenceEquals(x.ApplicationRegistry, dxf.ApplicationRegistries[x.ApplicationRegistry.Name]), "Object reference not equal.");
                    }
                }
            }
            Console.WriteLine();

            Console.WriteLine("LAYOUTS: {0}", dxf.Layouts.Count);
            foreach (var o in dxf.Layouts)
            {
                Debug.Assert(ReferenceEquals(o.AssociatedBlock, dxf.Blocks[o.AssociatedBlock.Name]), "Object reference not equal.");

                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Layouts.GetReferences(o.Name).Count);
                List<DxfObject> entities = dxf.Layouts.GetReferences(o.Name);
                foreach (var e in entities)
                {
                    EntityObject entity = e as EntityObject;
                    if (entity != null)
                    {
                        Debug.Assert(ReferenceEquals(entity.Layer, dxf.Layers[entity.Layer.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(entity.Linetype, dxf.Linetypes[entity.Linetype.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(entity.Owner, dxf.Blocks[o.AssociatedBlock.Name]), "Object reference not equal.");
                        foreach (var x in entity.XData.Values)
                        {
                            Debug.Assert(ReferenceEquals(x.ApplicationRegistry, dxf.ApplicationRegistries[x.ApplicationRegistry.Name]), "Object reference not equal.");
                        }
                    }

                    Text txt = e as Text;
                    if (txt != null) Debug.Assert(ReferenceEquals(txt.Style, dxf.TextStyles[txt.Style.Name]), "Object reference not equal.");

                    MText mtxt = e as MText;
                    if (mtxt != null) Debug.Assert(ReferenceEquals(mtxt.Style, dxf.TextStyles[mtxt.Style.Name]), "Object reference not equal.");

                    Dimension dim = e as Dimension;
                    if (dim != null)
                    {
                        Debug.Assert(ReferenceEquals(dim.Style, dxf.DimensionStyles[dim.Style.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(dim.Block, dxf.Blocks[dim.Block.Name]), "Object reference not equal.");
                    }

                    MLine mline = e as MLine;
                    if (mline != null) Debug.Assert(ReferenceEquals(mline.Style, dxf.MlineStyles[mline.Style.Name]), "Object reference not equal.");

                    Image img = e as Image;
                    if (img != null) Debug.Assert(ReferenceEquals(img.Definition, dxf.ImageDefinitions[img.Definition.Name]), "Object reference not equal.");

                    Insert ins = e as Insert;
                    if (ins != null)
                    {
                        Debug.Assert(ReferenceEquals(ins.Block, dxf.Blocks[ins.Block.Name]), "Object reference not equal.");
                        foreach (var a in ins.Attributes)
                        {
                            Debug.Assert(ReferenceEquals(a.Layer, dxf.Layers[a.Layer.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Linetype, dxf.Linetypes[a.Linetype.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Style, dxf.TextStyles[a.Style.Name]), "Object reference not equal.");
                        }
                    }
                }
            }
            Console.WriteLine();

            Console.WriteLine("IMAGE DEFINITIONS: {0}", dxf.ImageDefinitions.Count);
            foreach (var o in dxf.ImageDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.ImageDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("DGN UNDERLAY DEFINITIONS: {0}", dxf.UnderlayDgnDefinitions.Count);
            foreach (var o in dxf.UnderlayDgnDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.UnderlayDgnDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("DWF UNDERLAY DEFINITIONS: {0}", dxf.UnderlayDwfDefinitions.Count);
            foreach (var o in dxf.UnderlayDwfDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.UnderlayDwfDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("PDF UNDERLAY DEFINITIONS: {0}", dxf.UnderlayPdfDefinitions.Count);
            foreach (var o in dxf.UnderlayPdfDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.UnderlayPdfDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("GROUPS: {0}", dxf.Groups.Count);
            foreach (var o in dxf.Groups)
            {
                Console.WriteLine("\t{0}; Entities count: {1}", o.Name, o.Entities.Count);
            }
            Console.WriteLine();

            // the entities lists contain the geometry that has a graphical representation in the drawing across all layouts,
            // to get the entities that belongs to a specific layout you can get the references through the Layouts.GetReferences(name)
            // or check the EntityObject.Owner.Record.Layout property
            Console.WriteLine("ENTITIES:");
            Console.WriteLine("\t{0}; count: {1}", EntityType.Arc, dxf.Arcs.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.AttributeDefinition, dxf.AttributeDefinitions.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Circle, dxf.Circles.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Dimension, dxf.Dimensions.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Ellipse, dxf.Ellipses.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Face3D, dxf.Faces3d.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Hatch, dxf.Hatches.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Image, dxf.Images.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Insert, dxf.Inserts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Leader, dxf.Leaders.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.LightWeightPolyline, dxf.LwPolylines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Line, dxf.Lines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Mesh, dxf.Meshes.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.MLine, dxf.MLines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.MText, dxf.MTexts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Point, dxf.Points.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.PolyfaceMesh, dxf.PolyfaceMeshes.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Polyline, dxf.Polylines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Solid, dxf.Solids.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Spline, dxf.Splines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Text, dxf.Texts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Ray, dxf.Rays.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Underlay, dxf.Underlays.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Viewport, dxf.Viewports.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Wipeout, dxf.Wipeouts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.XLine, dxf.XLines.Count);
            Console.WriteLine();

            Console.WriteLine("Press a key to continue...");
            Console.ReadLine();
        }

        public static DxfDocument Test(string file, string output = null)
        {
            // optionally you can save the information to a text file
            bool outputLog = !string.IsNullOrEmpty(output);
            TextWriter writer = null;
            if (outputLog)
            {
                writer = new StreamWriter(File.Create(output));
                Console.SetOut(writer);
            }

            // check if the dxf actually exists
            FileInfo fileInfo = new FileInfo(file);

            if (!fileInfo.Exists)
            {
                Console.WriteLine("THE FILE {0} DOES NOT EXIST", file);
                Console.WriteLine();

                if (outputLog)
                {
                    writer.Flush();
                    writer.Close();
                }
                else
                {
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadLine();
                }
                return null;
            }
            bool isBinary;
            DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file, out isBinary);

            // check if the file is a dxf
            if (dxfVersion == DxfVersion.Unknown)
            {
                Console.WriteLine("THE FILE {0} IS NOT A VALID DXF OR THE DXF DOES NOT INCLUDE VERSION INFORMATION IN THE HEADER SECTION", file);
                Console.WriteLine();

                if (outputLog)
                {
                    writer.Flush();
                    writer.Close();
                }
                else
                {
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadLine();
                }
                return null;
            }

            // check if the dxf file version is supported
            if (dxfVersion < DxfVersion.AutoCad2000)
            {
                Console.WriteLine("THE FILE {0} IS NOT A SUPPORTED DXF", file);
                Console.WriteLine();

                Console.WriteLine("FILE VERSION: {0}", dxfVersion);
                Console.WriteLine();

                if (outputLog)
                {
                    writer.Flush();
                    writer.Close();
                }
                else
                {
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadLine();
                }
                return null;
            }

            DxfDocument dxf = DxfDocument.Load(file);

            // check if there has been any problems loading the file,
            // this might be the case of a corrupt file or a problem in the library
            if (dxf == null)
            {
                Console.WriteLine("ERROR LOADING {0}", file);
                Console.WriteLine();

                Console.WriteLine("Press a key to continue...");
                Console.ReadLine();

                if (outputLog)
                {
                    writer.Flush();
                    writer.Close();
                }
                else
                {
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadLine();
                }
                return null;
            }

            // the dxf has been properly loaded, let's show some information about it
            Console.WriteLine("FILE NAME: {0}", file);
            Console.WriteLine("\tbinary dxf: {0}", isBinary);
            Console.WriteLine();
            Console.WriteLine("FILE VERSION: {0}", dxf.DrawingVariables.AcadVer);
            Console.WriteLine();
            Console.WriteLine("FILE COMMENTS: {0}", dxf.Comments.Count);
            foreach (var o in dxf.Comments)
            {
                Console.WriteLine("\t{0}", o);
            }
            Console.WriteLine();
            Console.WriteLine("FILE TIME:");
            Console.WriteLine("\tdrawing created (UTC): {0}.{1}", dxf.DrawingVariables.TduCreate, dxf.DrawingVariables.TduCreate.Millisecond.ToString("000"));
            Console.WriteLine("\tdrawing last update (UTC): {0}.{1}", dxf.DrawingVariables.TduUpdate, dxf.DrawingVariables.TduUpdate.Millisecond.ToString("000"));
            Console.WriteLine("\tdrawing edition time: {0}", dxf.DrawingVariables.TdinDwg);
            Console.WriteLine();
            Console.WriteLine("APPLICATION REGISTRIES: {0}", dxf.ApplicationRegistries.Count);
            foreach (var o in dxf.ApplicationRegistries)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.ApplicationRegistries.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("LAYERS: {0}", dxf.Layers.Count);
            foreach (var o in dxf.Layers)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Layers.GetReferences(o).Count);
                Debug.Assert(ReferenceEquals(o.Linetype, dxf.Linetypes[o.Linetype.Name]), "Object reference not equal.");
            }
            Console.WriteLine();

            Console.WriteLine("LINE TYPES: {0}", dxf.Linetypes.Count);
            foreach (var o in dxf.Linetypes)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Linetypes.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("TEXT STYLES: {0}", dxf.TextStyles.Count);
            foreach (var o in dxf.TextStyles)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.TextStyles.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("DIMENSION STYLES: {0}", dxf.DimensionStyles.Count);
            foreach (var o in dxf.DimensionStyles)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.DimensionStyles.GetReferences(o.Name).Count);
                Debug.Assert(ReferenceEquals(o.TextStyle, dxf.TextStyles[o.TextStyle.Name]), "Object reference not equal.");
                Debug.Assert(ReferenceEquals(o.DimLineLinetype, dxf.Linetypes[o.DimLineLinetype.Name]), "Object reference not equal.");
                Debug.Assert(ReferenceEquals(o.ExtLine1Linetype, dxf.Linetypes[o.ExtLine1Linetype.Name]), "Object reference not equal.");
                Debug.Assert(ReferenceEquals(o.ExtLine2Linetype, dxf.Linetypes[o.ExtLine2Linetype.Name]), "Object reference not equal.");
                if (o.DimArrow1 != null) Debug.Assert(ReferenceEquals(o.DimArrow1, dxf.Blocks[o.DimArrow1.Name]), "Object reference not equal.");
                if (o.DimArrow2 != null) Debug.Assert(ReferenceEquals(o.DimArrow2, dxf.Blocks[o.DimArrow2.Name]), "Object reference not equal.");
            }
            Console.WriteLine();

            Console.WriteLine("MLINE STYLES: {0}", dxf.MlineStyles.Count);
            foreach (var o in dxf.MlineStyles)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.MlineStyles.GetReferences(o.Name).Count);
                foreach (var e in o.Elements)
                {
                    Debug.Assert(ReferenceEquals(e.Linetype, dxf.Linetypes[e.Linetype.Name]), "Object reference not equal.");
                }
            }
            Console.WriteLine();

            Console.WriteLine("UCSs: {0}", dxf.UCSs.Count);
            foreach (var o in dxf.UCSs)
            {
                Console.WriteLine("\t{0}", o.Name);
            }
            Console.WriteLine();

            Console.WriteLine("BLOCKS: {0}", dxf.Blocks.Count);
            foreach (var o in dxf.Blocks)
            {
                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Blocks.GetReferences(o.Name).Count);
                Debug.Assert(ReferenceEquals(o.Layer, dxf.Layers[o.Layer.Name]), "Object reference not equal.");

                foreach (var e in o.Entities)
                {
                    Debug.Assert(ReferenceEquals(e.Layer, dxf.Layers[e.Layer.Name]), "Object reference not equal.");
                    Debug.Assert(ReferenceEquals(e.Linetype, dxf.Linetypes[e.Linetype.Name]), "Object reference not equal.");
                    Debug.Assert(ReferenceEquals(e.Owner, dxf.Blocks[o.Name]), "Object reference not equal.");
                    foreach (var x in e.XData.Values)
                    {
                        Debug.Assert(ReferenceEquals(x.ApplicationRegistry, dxf.ApplicationRegistries[x.ApplicationRegistry.Name]), "Object reference not equal.");
                    }

                    Text txt = e as Text;
                    if (txt != null) Debug.Assert(ReferenceEquals(txt.Style, dxf.TextStyles[txt.Style.Name]), "Object reference not equal.");

                    MText mtxt = e as MText;
                    if (mtxt != null) Debug.Assert(ReferenceEquals(mtxt.Style, dxf.TextStyles[mtxt.Style.Name]), "Object reference not equal.");

                    Dimension dim = e as Dimension;
                    if (dim != null)
                    {
                        Debug.Assert(ReferenceEquals(dim.Style, dxf.DimensionStyles[dim.Style.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(dim.Block, dxf.Blocks[dim.Block.Name]), "Object reference not equal.");
                    }

                    MLine mline = e as MLine;
                    if (mline != null) Debug.Assert(ReferenceEquals(mline.Style, dxf.MlineStyles[mline.Style.Name]), "Object reference not equal.");

                    Image img = e as Image;
                    if (img != null) Debug.Assert(ReferenceEquals(img.Definition, dxf.ImageDefinitions[img.Definition.Name]), "Object reference not equal.");

                    Insert ins = e as Insert;
                    if (ins != null)
                    {
                        Debug.Assert(ReferenceEquals(ins.Block, dxf.Blocks[ins.Block.Name]), "Object reference not equal.");
                        foreach (var a in ins.Attributes)
                        {
                            Debug.Assert(ReferenceEquals(a.Layer, dxf.Layers[a.Layer.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Linetype, dxf.Linetypes[a.Linetype.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Style, dxf.TextStyles[a.Style.Name]), "Object reference not equal.");
                        }
                    }
                }

                foreach (var a in o.AttributeDefinitions.Values)
                {
                    Debug.Assert(ReferenceEquals(a.Layer, dxf.Layers[a.Layer.Name]), "Object reference not equal.");
                    Debug.Assert(ReferenceEquals(a.Linetype, dxf.Linetypes[a.Linetype.Name]), "Object reference not equal.");
                    foreach (var x in a.XData.Values)
                    {
                        Debug.Assert(ReferenceEquals(x.ApplicationRegistry, dxf.ApplicationRegistries[x.ApplicationRegistry.Name]), "Object reference not equal.");
                    }
                }
            }
            Console.WriteLine();

            Console.WriteLine("LAYOUTS: {0}", dxf.Layouts.Count);
            foreach (var o in dxf.Layouts)
            {
                Debug.Assert(ReferenceEquals(o.AssociatedBlock, dxf.Blocks[o.AssociatedBlock.Name]), "Object reference not equal.");

                Console.WriteLine("\t{0}; References count: {1}", o.Name, dxf.Layouts.GetReferences(o.Name).Count);
                List<DxfObject> entities = dxf.Layouts.GetReferences(o.Name);
                foreach (var e in entities)
                {
                    EntityObject entity = e as EntityObject;
                    if (entity != null)
                    {
                        Debug.Assert(ReferenceEquals(entity.Layer, dxf.Layers[entity.Layer.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(entity.Linetype, dxf.Linetypes[entity.Linetype.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(entity.Owner, dxf.Blocks[o.AssociatedBlock.Name]), "Object reference not equal.");
                        foreach (var x in entity.XData.Values)
                        {
                            Debug.Assert(ReferenceEquals(x.ApplicationRegistry, dxf.ApplicationRegistries[x.ApplicationRegistry.Name]), "Object reference not equal.");
                        }
                    }

                    Text txt = e as Text;
                    if (txt != null) Debug.Assert(ReferenceEquals(txt.Style, dxf.TextStyles[txt.Style.Name]), "Object reference not equal.");

                    MText mtxt = e as MText;
                    if (mtxt != null) Debug.Assert(ReferenceEquals(mtxt.Style, dxf.TextStyles[mtxt.Style.Name]), "Object reference not equal.");

                    Dimension dim = e as Dimension;
                    if (dim != null)
                    {
                        Debug.Assert(ReferenceEquals(dim.Style, dxf.DimensionStyles[dim.Style.Name]), "Object reference not equal.");
                        Debug.Assert(ReferenceEquals(dim.Block, dxf.Blocks[dim.Block.Name]), "Object reference not equal.");
                    }

                    MLine mline = e as MLine;
                    if (mline != null) Debug.Assert(ReferenceEquals(mline.Style, dxf.MlineStyles[mline.Style.Name]), "Object reference not equal.");

                    Image img = e as Image;
                    if (img != null) Debug.Assert(ReferenceEquals(img.Definition, dxf.ImageDefinitions[img.Definition.Name]), "Object reference not equal.");

                    Insert ins = e as Insert;
                    if (ins != null)
                    {
                        Debug.Assert(ReferenceEquals(ins.Block, dxf.Blocks[ins.Block.Name]), "Object reference not equal.");
                        foreach (var a in ins.Attributes)
                        {
                            Debug.Assert(ReferenceEquals(a.Layer, dxf.Layers[a.Layer.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Linetype, dxf.Linetypes[a.Linetype.Name]), "Object reference not equal.");
                            Debug.Assert(ReferenceEquals(a.Style, dxf.TextStyles[a.Style.Name]), "Object reference not equal.");
                        }
                    }
                }
            }
            Console.WriteLine();

            Console.WriteLine("IMAGE DEFINITIONS: {0}", dxf.ImageDefinitions.Count);
            foreach (var o in dxf.ImageDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.ImageDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("DGN UNDERLAY DEFINITIONS: {0}", dxf.UnderlayDgnDefinitions.Count);
            foreach (var o in dxf.UnderlayDgnDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.UnderlayDgnDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("DWF UNDERLAY DEFINITIONS: {0}", dxf.UnderlayDwfDefinitions.Count);
            foreach (var o in dxf.UnderlayDwfDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.UnderlayDwfDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("PDF UNDERLAY DEFINITIONS: {0}", dxf.UnderlayPdfDefinitions.Count);
            foreach (var o in dxf.UnderlayPdfDefinitions)
            {
                Console.WriteLine("\t{0}; File name: {1}; References count: {2}", o.Name, o.FileName, dxf.UnderlayPdfDefinitions.GetReferences(o.Name).Count);
            }
            Console.WriteLine();

            Console.WriteLine("GROUPS: {0}", dxf.Groups.Count);
            foreach (var o in dxf.Groups)
            {
                Console.WriteLine("\t{0}; Entities count: {1}", o.Name, o.Entities.Count);
            }
            Console.WriteLine();

            // the entities lists contain the geometry that has a graphical representation in the drawing across all layouts,
            // to get the entities that belongs to a specific layout you can get the references through the Layouts.GetReferences(name)
            // or check the EntityObject.Owner.Record.Layout property
            Console.WriteLine("ENTITIES:");
            Console.WriteLine("\t{0}; count: {1}", EntityType.Arc, dxf.Arcs.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.AttributeDefinition, dxf.AttributeDefinitions.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Circle, dxf.Circles.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Dimension, dxf.Dimensions.Count);
            foreach (var a in dxf.Dimensions)
            {
                foreach (var styleOverride in a.StyleOverrides.Values)
                {
                    switch (styleOverride.Type)
                    {
                        case DimensionStyleOverrideType.DimLineLinetype:
                            Debug.Assert(ReferenceEquals((Linetype)styleOverride.Value, dxf.Linetypes[((Linetype)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.ExtLine1Linetype:
                            Debug.Assert(ReferenceEquals((Linetype)styleOverride.Value, dxf.Linetypes[((Linetype)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.ExtLine2Linetype:
                            Debug.Assert(ReferenceEquals((Linetype)styleOverride.Value, dxf.Linetypes[((Linetype)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.TextStyle:
                            Debug.Assert(ReferenceEquals((TextStyle)styleOverride.Value, dxf.TextStyles[((TextStyle)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.LeaderArrow:
                            Debug.Assert(ReferenceEquals((Block)styleOverride.Value, dxf.Blocks[((Block)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.DimArrow1:
                            if (styleOverride.Value == null) break;
                            Debug.Assert(ReferenceEquals((Block)styleOverride.Value, dxf.Blocks[((Block)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.DimArrow2:
                            if (styleOverride.Value == null) break;
                            Debug.Assert(ReferenceEquals((Block)styleOverride.Value, dxf.Blocks[((Block)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                    }
                }
            }
            Console.WriteLine("\t{0}; count: {1}", EntityType.Ellipse, dxf.Ellipses.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Face3D, dxf.Faces3d.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Hatch, dxf.Hatches.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Image, dxf.Images.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Insert, dxf.Inserts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Leader, dxf.Leaders.Count);
            foreach (var a in dxf.Leaders)
            {
                foreach (var styleOverride in a.StyleOverrides.Values)
                {
                    switch (styleOverride.Type)
                    {
                        case DimensionStyleOverrideType.DimLineLinetype:
                            Debug.Assert(ReferenceEquals((Linetype)styleOverride.Value, dxf.Linetypes[((Linetype)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.ExtLine1Linetype:
                            Debug.Assert(ReferenceEquals((Linetype)styleOverride.Value, dxf.Linetypes[((Linetype)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.ExtLine2Linetype:
                            Debug.Assert(ReferenceEquals((Linetype)styleOverride.Value, dxf.Linetypes[((Linetype)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.TextStyle:
                            Debug.Assert(ReferenceEquals((TextStyle)styleOverride.Value, dxf.TextStyles[((TextStyle)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.LeaderArrow:
                            Debug.Assert(ReferenceEquals((Block)styleOverride.Value, dxf.Blocks[((Block)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.DimArrow1:
                            Debug.Assert(ReferenceEquals((Block)styleOverride.Value, dxf.Blocks[((Block)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                        case DimensionStyleOverrideType.DimArrow2:
                            Debug.Assert(ReferenceEquals((Block)styleOverride.Value, dxf.Blocks[((Block)styleOverride.Value).Name]), "Object reference not equal.");
                            break;
                    }
                }
            }
            Console.WriteLine("\t{0}; count: {1}", EntityType.LightWeightPolyline, dxf.LwPolylines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Line, dxf.Lines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Mesh, dxf.Meshes.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.MLine, dxf.MLines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.MText, dxf.MTexts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Point, dxf.Points.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.PolyfaceMesh, dxf.PolyfaceMeshes.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Polyline, dxf.Polylines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Solid, dxf.Solids.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Spline, dxf.Splines.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Text, dxf.Texts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Ray, dxf.Rays.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Underlay, dxf.Underlays.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Viewport, dxf.Viewports.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.Wipeout, dxf.Wipeouts.Count);
            Console.WriteLine("\t{0}; count: {1}", EntityType.XLine, dxf.XLines.Count);
            Console.WriteLine();

            // the dxf version is controlled by the DrawingVariables property of the dxf document,
            // also a HeaderVariables instance or a DxfVersion can be passed to the constructor to initialize a new DxfDocument.
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2018;
            dxf.Save("sample 2018.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2013;
            dxf.Save("sample 2013.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            dxf.Save("sample 2010.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save("sample 2007.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2004;
            dxf.Save("sample 2004.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000;
            dxf.Save("sample 2000.dxf");

            // saving to binary dxf
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2013;
            dxf.Save("binary test.dxf", true);
            DxfDocument test = DxfDocument.Load("binary test.dxf");

            if (outputLog)
            {
                writer.Flush();
                writer.Close();
            }
            else
            {
                Console.WriteLine("Press a key to continue...");
                Console.ReadLine();
            }
            return dxf;
        }

        public static void drawPline(DxfDocument dxf, Vector2[] v2Arr, double Width, netDxf.Tables.Layer layercolor)  //画多点线段，参数1=dxf文件名，参数2=二维点数组，参数3=颜色
        {

            v2Arr.GetValue(0);

            for (int i = 0; i < v2Arr.Length - 1; i++)
            {

                Line line = new Line((Vector2)v2Arr.GetValue(i), (Vector2)v2Arr.GetValue(i + 1));
                line.Layer = layercolor;
                dxf.AddEntity(line);
            }
        }

        public static void drawPlinetype(DxfDocument dxf, Vector2[] v2Arr, double Width, netDxf.Tables.Layer layercolor, Linetype type)  //画不同线型的线段，参数1=dxf文件名，参数2=二维点数组，参数3=颜色/图层，参数4=线型
        {
            v2Arr.GetValue(0);
            {
                Line line = new Line((Vector2)v2Arr.GetValue(0), (Vector2)v2Arr.GetValue(1));
                line.Layer = layercolor;
                line.Linetype = type;
                dxf.AddEntity(line);
            }
        }

        public static void drawPlineOfw(DxfDocument dxf, Vector2[] v2Arr, double Width, netDxf.Tables.Layer layercolor)  //画不同线宽的线段，参数1=dxf文件名，参数2=二维点数组，参数3=线宽，参数4=颜色/图层
        {

            v2Arr.GetValue(0);

            for (int i = 0; i < v2Arr.Length - 1; i++)
            {
                LwPolyline poly = new LwPolyline();
                poly.Vertexes.Add(new LwPolylineVertex((Vector2)v2Arr.GetValue(i)));
                poly.Vertexes.Add(new LwPolylineVertex((Vector2)v2Arr.GetValue(i + 1)));
                poly.SetConstantWidth(Width);
                poly.Layer = layercolor;
                dxf.AddEntity(poly);
            }
        }


        //边框全由线段组成，进行填充，参数1=dxf文件名，参数2=类型数组，参数3=元素数量，参数4=剖面线倾斜角度，参数5=剖面线间距，参数6=线段数组，参数7=颜色/图层
        /* 填充前的前置参数设置，举例
            String[] type = new String[100];
            Line[] line = new Line[100];
            int numst = poly.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < numst; i++)
            {
                type[i] = poly.Explode()[i].Type.ToString();    //将所有元素的类型记录成一个数组
                if (type[i] == "Line")
                {
                    line[i] = (Line)poly.Explode()[i];
                }
            }
        */
        public static void hatchsubofline(DxfDocument dxf1, String[] type, int numst, double pmxangle, double pmxdist, Line[] line, netDxf.Tables.Layer layercolor)
        {
            //            Line[] line = new Line[1000];
            //            Arc[] arc = new Arc[1000];
            //            Circle[] circle = new Circle[1000];
            Vector2[] startpoint = new Vector2[1000];
            Vector2[] endpoint = new Vector2[1000];
            Vector2[] tangentpoint = new Vector2[1000];
            Vector2[] center = new Vector2[1000];
            double[] startangle = new double[1000];
            double[] endangle = new double[1000];
            double[] radius = new double[1000];
            double[] radiusmax = new double[1000];
            double[] radiusmin = new double[1000];
            //            Ellipse[] ellipse = new Ellipse[1000];
            //            String[] type = new String[1000];

            double[] b = new double[1000];
            double[] bpmx = new double[1000];
            double[] tt = new double[1000];
            double t = Math.Tan(pmxangle * Math.PI / 180.0);
            double bm = -10000.0, bl = 10000.0;
            double jdx, jdy;
            double angjd;
            double aa, bb, cc, dd;  //??

            int nl;
            double h = Math.Abs(pmxdist / Math.Cos(pmxangle * Math.PI / 180.0) / 8.0);
            for (int i = 0; i < numst; i++)
            {
                if (type[i] == "Line")
                {
                    startpoint[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    endpoint[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                    {
                        tt[i] = 100000000.0;   //tt指直线斜率
                    }
                    else
                    {
                        tt[i] = (endpoint[i].Y - startpoint[i].Y) / (endpoint[i].X - startpoint[i].X);
                    }
                    b[i] = startpoint[i].Y - t * startpoint[i].X;  //??
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];
                }

            }
            nl = (int)((bm - bl) / h);//剖面线条数
            int yxjds;//有效交点数

            Line[] lineyx = new Line[100];
            double[] xyx = new double[100];
            double[] yxy = new double[100];
            for (int j = 1; j < nl; j++)
            {
                yxjds = 0;
                bpmx[j] = bl + h * j;
                for (int i = 0; i <= numst; i++)
                {
                    if (type[i] == "Line")
                    {
                        if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                        {
                            jdx = startpoint[i].X;
                            jdy = bpmx[j] + t * jdx;
                        }
                        else
                        {
                            jdx = (startpoint[i].Y - bpmx[j] - tt[i] * startpoint[i].X) / (t - tt[i]);
                            jdy = bpmx[j] + t * jdx;
                        }
                        if (distance1(new Vector2(jdx, jdy), startpoint[i]) <= distance1(startpoint[i], endpoint[i]) &&
                            distance1(new Vector2(jdx, jdy), endpoint[i]) < distance1(startpoint[i], endpoint[i]))
                        {
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }
                    if (type[i] == "Arc")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd = 90;
                                else angjd = -90;
                            }
                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            if (angjd <= endangle[i] && angjd >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }

                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd = 90;
                                else angjd = -90;
                            }
                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            if (angjd <= endangle[i] && angjd >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }
                    }
                    if (type[i] == "Circle")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;


                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }

                }

                //重新排序
                double tempx, tempy;
                for (int jj = 1; jj <= yxjds; jj++)
                {
                    for (int ii = 1; ii <= yxjds - 1; ii++)
                        if (xyx[ii + 1] < xyx[ii])
                        {
                            tempx = xyx[ii];
                            tempy = yxy[ii];
                            xyx[ii] = xyx[ii + 1];
                            yxy[ii] = yxy[ii + 1];
                            xyx[ii + 1] = tempx;
                            yxy[ii + 1] = tempy;
                        }
                }
                if ((int)(yxjds / 2) == (yxjds / 2))    //偶数个交点
                {
                    for (int kk = 1; kk <= yxjds / 2; kk++)
                    {
                        lineyx[kk] = new Line(new Vector2(xyx[kk * 2 - 1], yxy[kk * 2 - 1]), new Vector2(xyx[kk * 2], yxy[kk * 2]));
                        lineyx[kk].Layer = layercolor;
                        dxf1.AddEntity(lineyx[kk]);
                    }
                }

            }
        }

        //边框由线段，圆弧，圆，椭圆组成，进行填充，参数1=dxf文件名，参数2=类型数组，参数3=元素数量，参数4=剖面线倾斜角度，参数5=剖面线间距，参数6=线段数组，参数7=圆弧数组，参数8=圆数组，参数9=椭圆数组，参数10=椭圆角度，参数11=？，参数12=？
        //具体参数设置参考hatchtest2
        public static void hatchsub(DxfDocument dxf1, String[] type, int numst, double pmxangle, double pmxdist, Line[] line, Arc[] arc, Circle[] circle, Ellipse[] ellipse, double tyrotangle, double tys, double tyt)
        {
            //            Line[] line = new Line[1000];
            //            Arc[] arc = new Arc[1000];
            //            Circle[] circle = new Circle[1000];
            Vector2[] startpoint = new Vector2[1000];
            Vector2[] endpoint = new Vector2[1000];

            Vector2[] tangentpoint = new Vector2[1000];
            Vector2[] center = new Vector2[1000];
            double[] startangle = new double[1000];
            double[] endangle = new double[1000];
            double[] radius = new double[1000];
            double[] radiusmax = new double[1000];
            double[] radiusmin = new double[1000];
            //            Ellipse[] ellipse = new Ellipse[1000];
            //            String[] type = new String[1000];

            double[] b = new double[1000];
            double[] bpmx = new double[1000];
            double[] tt = new double[1000];
            double t = Math.Tan(pmxangle * Math.PI / 180.0);
            double bm = -10000.0, bl = 10000.0;
            double jdx, jdy;
            double angjd = 0.0, angjd1 = 0.0;
            double aa, bb, cc, dd;
            //            int numst=0;
            double tangentangle;
            double tangentangle1;

            int nl;
            double h = Math.Abs(pmxdist / Math.Cos(pmxangle * Math.PI / 180.0) / 8.0);
            for (int i = 0; i <= numst; i++)
            {
                if (type[i] == "Line")
                {
                    startpoint[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    endpoint[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001) { tt[i] = 100000000.0; }
                    else { tt[i] = (endpoint[i].Y - startpoint[i].Y) / (endpoint[i].X - startpoint[i].X); }
                    b[i] = startpoint[i].Y - t * startpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];
                }
                if (type[i] == "Arc")
                {
                    //                    arc[i] = (Arc)poly.Explode()[i];
                    center[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    startangle[i] = arc[i].StartAngle;
                    endangle[i] = arc[i].EndAngle;
                    //                    if (startangle[i] > endangle[i]) endangle[i] = endangle[i] + 360;


                    radius[i] = arc[i].Radius;

                    startpoint[i] = Vector2.Polar(center[i], radius[i], startangle[i] * Math.PI / 180);
                    endpoint[i] = Vector2.Polar(center[i], radius[i], endangle[i] * Math.PI / 180);

                    tangentangle = pmxangle + 90;

                    if (tangentangle >= startangle[i] && tangentangle <= endangle[i]) tangentangle1 = tangentangle;
                    else tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);


                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                    tangentangle = pmxangle - 90;

                    if (tangentangle >= startangle[i] && tangentangle <= endangle[i]) tangentangle1 = tangentangle;
                    else tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);


                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];


                }
                if (type[i] == "Circle")
                {
                    //                    circle[i] = circle1;
                    center[i] = new Vector2(circle[i].Center.X, circle[i].Center.Y);
                    radius[i] = circle[i].Radius;

                    tangentangle = pmxangle + 90;
                    tangentangle1 = tangentangle;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);

                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                    tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);

                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                }
                if (type[i] == "Ellipse")
                {
                    //                    circle[i] = circle1;
                    center[i] = new Vector2(ellipse[i].Center.X, ellipse[i].Center.Y);
                    radiusmax[i] = ellipse[i].MajorAxis / 2.0;
                    radiusmin[i] = ellipse[i].MinorAxis / 2.0;
                    startangle[i] = ellipse[i].StartAngle;
                    endangle[i] = ellipse[i].EndAngle;

                    tangentangle = pmxangle + 90;
                    tangentangle1 = tangentangle;

                    tangentpoint[i] = Vector2.Polar(center[i], radiusmax[i], tangentangle1 * Math.PI / 180);

                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                    tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radiusmax[i], tangentangle1 * Math.PI / 180);

                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                }
            }
            nl = (int)((bm - bl) / h);//剖面线条数
            int yxjds;//有效交点数

            Line[] lineyx = new Line[100];
            double[] xyx = new double[100];
            double[] yxy = new double[100];
            for (int j = 1; j < nl; j++)
            {
                yxjds = 0;
                bpmx[j] = bl + h * j;
                for (int i = 0; i <= numst; i++)
                {
                    if (type[i] == "Line")
                    {
                        if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                        {
                            jdx = startpoint[i].X;
                            jdy = bpmx[j] + t * jdx;
                        }
                        else
                        {
                            jdx = (startpoint[i].Y - bpmx[j] - tt[i] * startpoint[i].X) / (t - tt[i]);
                            jdy = bpmx[j] + t * jdx;
                        }
                        if (distance1(new Vector2(jdx, jdy), startpoint[i]) <= distance1(startpoint[i], endpoint[i]) &&
                            distance1(new Vector2(jdx, jdy), endpoint[i]) < distance1(startpoint[i], endpoint[i]))
                        {
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }
                    if (type[i] == "Arc")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd1 = 90;
                                else angjd1 = 270;
                            }
                            //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;

                            else
                            {
                                angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                            }

                            if (startangle[i] > endangle[i])
                            {
                                if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            else
                            {
                                if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd1 = 90;
                                else angjd1 = 270;
                            }
                            //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            else
                            {
                                angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                            }

                            if (startangle[i] > endangle[i])
                            {
                                if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            else
                            {
                                if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                        }
                    }
                    if (type[i] == "Circle")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;


                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }
                    if (type[i] == "Ellipse")
                    {
                        aa = Math.Pow((Math.Cos(tyrotangle * Math.PI / 180) + t * Math.Sin(tyrotangle * Math.PI / 180)), 2.0) / Math.Pow(radiusmax[i], 2);
                        aa = aa + Math.Pow((Math.Sin(tyrotangle * Math.PI / 180) - t * Math.Cos(tyrotangle * Math.PI / 180)), 2.0) / Math.Pow(radiusmin[i], 2);

                        bb = 2.0 * (Math.Cos(tyrotangle * Math.PI / 180) + t * Math.Sin(tyrotangle * Math.PI / 180)) * (tys * -1.0 * Math.Cos(tyrotangle * Math.PI / 180) + (bpmx[j] - tyt) * Math.Sin(tyrotangle * Math.PI / 180)) / Math.Pow(radiusmax[i], 2);
                        bb = bb + 2.0 * (Math.Sin(tyrotangle * Math.PI / 180) - t * Math.Cos(tyrotangle * Math.PI / 180)) * (tys * -1.0 * Math.Sin(tyrotangle * Math.PI / 180) - (bpmx[j] - tyt) * Math.Cos(tyrotangle * Math.PI / 180)) / Math.Pow(radiusmin[i], 2);

                        cc = Math.Pow((tys * -1.0 * Math.Cos(tyrotangle * Math.PI / 180) + (bpmx[j] - tyt) * Math.Sin(tyrotangle * Math.PI / 180)) / radiusmax[i], 2.0) + Math.Pow((-1.0 * tys * Math.Sin(tyrotangle * Math.PI / 180) - (-1.0 * tyt + bpmx[j]) * Math.Cos(tyrotangle * Math.PI / 180)) / radiusmin[i], 2.0) - 1;


                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd = 90;
                                else angjd = 270;
                            }
                            //                   else angjd = Math.Atan(jdy-center[i].Y/(jdx-center[i].X))*180/Math.PI;
                            else
                            {
                                angjd = Math.Acos(Math.Abs(jdx - center[i].X) / Math.Sqrt(Math.Pow(jdx - center[i].X, 2.0) + Math.Pow(jdy - center[i].Y, 2.0))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd = 360 - angjd;
                            }
                            if (angjd <= endangle[i] && angjd >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }

                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd = 90;
                                else angjd = 270;
                            }
                            //                   else angjd = Math.Atan(jdy-center[i].Y/(jdx-center[i].X))*180/Math.PI;
                            else
                            {
                                angjd = Math.Acos(Math.Abs(jdx - center[i].X) / Math.Sqrt(Math.Pow(jdx - center[i].X, 2.0) + Math.Pow(jdy - center[i].Y, 2.0))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd = 360 - angjd;
                            }
                            if (angjd <= endangle[i] && angjd >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }
                    }
                }

                //重新排序
                double tempx, tempy;
                for (int jj = 1; jj <= yxjds; jj++)
                {
                    for (int ii = 1; ii <= yxjds - 1; ii++)
                        if (xyx[ii + 1] < xyx[ii])
                        {
                            tempx = xyx[ii];
                            tempy = yxy[ii];
                            xyx[ii] = xyx[ii + 1];
                            yxy[ii] = yxy[ii + 1];
                            xyx[ii + 1] = tempx;
                            yxy[ii + 1] = tempy;
                        }
                }

                //               int bjz1 = 10*(int)(yxjds / 2);
                //               int bjz2 = (int)(10.0*yxjds / 2.0);

                if (10 * (int)(yxjds / 2) == (int)(10.0 * yxjds / 2.0))//偶数个交点
                {
                    for (int kk = 1; kk <= yxjds / 2; kk++)
                    {
                        lineyx[kk] = new Line(new Vector2(xyx[kk * 2 - 1], yxy[kk * 2 - 1]), new Vector2(xyx[kk * 2], yxy[kk * 2]));
                        dxf1.AddEntity(lineyx[kk]);
                    }
                }


            }
        }

        //边框由线段，圆弧组成，参数多一个圆弧数组，前置参数设置参照hatchsubofline，增加圆弧数组即可
        public static void hatchsub1(DxfDocument dxf1, String[] type, int numst, double pmxangle, double pmxdist, Line[] line, Arc[] arc, netDxf.Tables.Layer layercolor)
        {
            Vector2[] startpoint = new Vector2[1000];
            Vector2[] endpoint = new Vector2[1000];

            Vector2[] tangentpoint = new Vector2[1000];
            Vector2[] center = new Vector2[1000];
            double[] startangle = new double[1000];
            double[] endangle = new double[1000];
            double[] radius = new double[1000];
            double[] radiusmax = new double[1000];
            double[] radiusmin = new double[1000];
            //            Ellipse[] ellipse = new Ellipse[1000];
            //            String[] type = new String[1000];

            double[] b = new double[1000];
            double[] bpmx = new double[1000];
            double[] tt = new double[1000];
            double t = Math.Tan(pmxangle * Math.PI / 180.0);
            double bm = -10000.0, bl = 10000.0;
            double jdx, jdy;
            double angjd = 0.0, angjd1 = 0.0;
            double aa, bb, cc, dd;
            //            int numst=0;
            double tangentangle;
            double tangentangle1;

            int nl;
            double h = Math.Abs(pmxdist / Math.Cos(pmxangle * Math.PI / 180.0) / 8.0);
            for (int i = 0; i <= numst; i++)
            {
                if (type[i] == "Line")
                {
                    startpoint[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    endpoint[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001) { tt[i] = 100000000.0; }
                    else { tt[i] = (endpoint[i].Y - startpoint[i].Y) / (endpoint[i].X - startpoint[i].X); }
                    b[i] = startpoint[i].Y - t * startpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];
                }
                if (type[i] == "Arc")
                {
                    //                    arc[i] = (Arc)poly.Explode()[i];
                    center[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    startangle[i] = arc[i].StartAngle;
                    endangle[i] = arc[i].EndAngle;
                    //                    if (startangle[i] > endangle[i]) endangle[i] = endangle[i] + 360;


                    radius[i] = arc[i].Radius;

                    startpoint[i] = Vector2.Polar(center[i], radius[i], startangle[i] * Math.PI / 180);
                    endpoint[i] = Vector2.Polar(center[i], radius[i], endangle[i] * Math.PI / 180);

                    tangentangle = pmxangle + 90;

                    if (tangentangle >= startangle[i] && tangentangle <= endangle[i]) tangentangle1 = tangentangle;
                    else tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);


                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                    tangentangle = pmxangle - 90;

                    if (tangentangle >= startangle[i] && tangentangle <= endangle[i]) tangentangle1 = tangentangle;
                    else tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);


                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];


                }

            }
            nl = (int)((bm - bl) / h);//剖面线条数
            int yxjds;//有效交点数

            Line[] lineyx = new Line[100];
            double[] xyx = new double[100];
            double[] yxy = new double[100];
            for (int j = 1; j < nl; j++)
            {
                yxjds = 0;
                bpmx[j] = bl + h * j;
                for (int i = 0; i <= numst; i++)
                {
                    if (type[i] == "Line")
                    {
                        if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                        {
                            jdx = startpoint[i].X;
                            jdy = bpmx[j] + t * jdx;
                        }
                        else
                        {
                            jdx = (startpoint[i].Y - bpmx[j] - tt[i] * startpoint[i].X) / (t - tt[i]);
                            jdy = bpmx[j] + t * jdx;
                        }
                        if (distance1(new Vector2(jdx, jdy), startpoint[i]) <= distance1(startpoint[i], endpoint[i]) &&
                            distance1(new Vector2(jdx, jdy), endpoint[i]) < distance1(startpoint[i], endpoint[i]))
                        {
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }
                    if (type[i] == "Arc")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd1 = 90;
                                else angjd1 = 270;
                            }
                            //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;

                            else
                            {
                                angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                            }

                            if (startangle[i] > endangle[i])
                            {
                                if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            else
                            {
                                if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }

                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd1 = 90;
                                else angjd1 = 270;
                            }
                            //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            else
                            {
                                angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                            }

                            if (startangle[i] > endangle[i])
                            {
                                if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            else
                            {
                                if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                        }
                    }

                }

                //重新排序
                double tempx, tempy;
                for (int jj = 1; jj <= yxjds; jj++)
                {
                    for (int ii = 1; ii <= yxjds - 1; ii++)
                        if (xyx[ii + 1] < xyx[ii])
                        {
                            tempx = xyx[ii];
                            tempy = yxy[ii];
                            xyx[ii] = xyx[ii + 1];
                            yxy[ii] = yxy[ii + 1];
                            xyx[ii + 1] = tempx;
                            yxy[ii + 1] = tempy;
                        }
                }

                //               int bjz1 = 10*(int)(yxjds / 2);
                //               int bjz2 = (int)(10.0*yxjds / 2.0);

                if (10 * (int)(yxjds / 2) == (int)(10.0 * yxjds / 2.0))//偶数个交点
                {
                    for (int kk = 1; kk <= yxjds / 2; kk++)
                    {
                        lineyx[kk] = new Line(new Vector2(xyx[kk * 2 - 1], yxy[kk * 2 - 1]), new Vector2(xyx[kk * 2], yxy[kk * 2]));
                        lineyx[kk].Layer = layercolor;
                        dxf1.AddEntity(lineyx[kk]);
                    }
                }


            }
        }
        public static void hatchsuboflinew(DxfDocument dxf1, String[] type, int numst, double pmxangle, double pmxdist, Line[] line, netDxf.Tables.Layer layercolor, double width)
        {
            //            Line[] line = new Line[1000];
            //            Arc[] arc = new Arc[1000];
            //            Circle[] circle = new Circle[1000];
            Vector2[] startpoint = new Vector2[1000];
            Vector2[] endpoint = new Vector2[1000];
            Vector2[] tangentpoint = new Vector2[1000];
            Vector2[] center = new Vector2[1000];
            double[] startangle = new double[1000];
            double[] endangle = new double[1000];
            double[] radius = new double[1000];
            double[] radiusmax = new double[1000];
            double[] radiusmin = new double[1000];
            //            Ellipse[] ellipse = new Ellipse[1000];
            //            String[] type = new String[1000];

            double[] b = new double[1000];
            double[] bpmx = new double[1000];
            double[] tt = new double[1000];
            double t = Math.Tan(pmxangle * Math.PI / 180.0);
            double bm = -10000.0, bl = 10000.0;
            double jdx, jdy;
            double angjd;
            double aa, bb, cc, dd;  //??

            int nl;
            double h = Math.Abs(pmxdist / Math.Cos(pmxangle * Math.PI / 180.0) / 8.0);
            for (int i = 0; i < numst; i++)
            {
                if (type[i] == "Line")
                {
                    startpoint[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    endpoint[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                    {
                        tt[i] = 100000000.0;   //tt指直线斜率
                    }
                    else
                    {
                        tt[i] = (endpoint[i].Y - startpoint[i].Y) / (endpoint[i].X - startpoint[i].X);
                    }
                    b[i] = startpoint[i].Y - t * startpoint[i].X;  //??
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];
                }

            }
            nl = (int)((bm - bl) / h);//剖面线条数
            int yxjds;//有效交点数

            LwPolyline[] lineyx = new LwPolyline[100];
            double[] xyx = new double[100];
            double[] yxy = new double[100];
            for (int j = 1; j < nl; j++)
            {
                yxjds = 0;
                bpmx[j] = bl + h * j;
                for (int i = 0; i <= numst; i++)
                {
                    if (type[i] == "Line")
                    {
                        if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                        {
                            jdx = startpoint[i].X;
                            jdy = bpmx[j] + t * jdx;
                        }
                        else
                        {
                            jdx = (startpoint[i].Y - bpmx[j] - tt[i] * startpoint[i].X) / (t - tt[i]);
                            jdy = bpmx[j] + t * jdx;
                        }
                        if (distance1(new Vector2(jdx, jdy), startpoint[i]) <= distance1(startpoint[i], endpoint[i]) &&
                            distance1(new Vector2(jdx, jdy), endpoint[i]) < distance1(startpoint[i], endpoint[i]))
                        {
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }
                    if (type[i] == "Arc")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd = 90;
                                else angjd = -90;
                            }
                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            if (angjd <= endangle[i] && angjd >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }

                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd = 90;
                                else angjd = -90;
                            }
                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            if (angjd <= endangle[i] && angjd >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }
                    }
                    if (type[i] == "Circle")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;


                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }

                }

                //重新排序
                double tempx, tempy;
                for (int jj = 1; jj <= yxjds; jj++)
                {
                    for (int ii = 1; ii <= yxjds - 1; ii++)
                        if (xyx[ii + 1] < xyx[ii])
                        {
                            tempx = xyx[ii];
                            tempy = yxy[ii];
                            xyx[ii] = xyx[ii + 1];
                            yxy[ii] = yxy[ii + 1];
                            xyx[ii + 1] = tempx;
                            yxy[ii + 1] = tempy;
                        }
                }
                if ((int)(yxjds / 2) == (yxjds / 2))    //偶数个交点
                {
                    for (int kk = 1; kk <= yxjds / 2; kk++)
                    {
                        lineyx[kk] = new LwPolyline();
                        lineyx[kk].Vertexes.Add(new LwPolylineVertex(new Vector2(xyx[kk * 2 - 1], yxy[kk * 2 - 1])));
                        lineyx[kk].Vertexes.Add(new LwPolylineVertex(new Vector2(xyx[kk * 2], yxy[kk * 2])));
                        lineyx[kk].Layer = layercolor;
                        lineyx[kk].SetConstantWidth(width);
                        dxf1.AddEntity(lineyx[kk]);
                    }
                }

            }
        }

        /*
        public static void PolyFill(DxfDocument dxf, LwPolyline poly, double width, netDxf.Tables.Layer layercolor)  //粗线效果
        {
            int numst1 = poly.Explode().Count;
            int numst = numst1;
            String[] type = new String[1000];
            Line[] line = new Line[1000];
            Arc[] arc = new Arc[1000];
            Vector2[] st = new Vector2[1000];
            Vector2[] ed = new Vector2[1000];
            Vector2[] st1 = new Vector2[1000];
            Vector2[] st2 = new Vector2[1000];
            Vector2[] ed1 = new Vector2[1000];
            Vector2[] ed2 = new Vector2[1000];
            double zj1;
            Vector2[] cen = new Vector2[1000];
            int dfs = 999;
            double stang, endang;

            for (int i = 0; i < numst; i++)
            {
                type[i] = poly.Explode()[i].Type.ToString();
                if (type[i] == "Line")
                {
                    line[i] = (Line)poly.Explode()[i];
                    st[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    ed[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);

                    if (ed[i].X == st[i].X)
                    {
                        if (ed[i].Y > st[i].Y) zj1 = 90;
                        else zj1 = -90;
                    }
                    else
                    {
                        zj1 = Math.Atan(Math.Abs((ed[i].Y - st[i].Y) / (ed[i].X - st[i].X))) * 180 / Math.PI;
                    }

                    if ((ed[i].X >= st[i].X) && (ed[i].Y > st[i].Y)) zj1 = zj1 * 1.0;//第一象限
                    if ((ed[i].X <= st[i].X) && (ed[i].Y > st[i].Y)) zj1 = 180.0 - zj1 * 1.0;//第二象限
                    if ((ed[i].X <= st[i].X) && (ed[i].Y < st[i].Y)) zj1 = 180.0 + zj1 * 1.0;//第三象限
                    if ((ed[i].X >= st[i].X) && (ed[i].Y < st[i].Y)) zj1 = 0.0 - zj1 * 1.0;//第四象限

                    st[i] = Vector2.Polar(st[i], width / 2, (zj1 + 180) * Math.PI / 180);
                    ed[i] = Vector2.Polar(ed[i], width / 2, zj1 * Math.PI / 180);
                    st1[i] = Vector2.Polar(st[i], width / 2, (zj1 + 90) * Math.PI / 180);
                    st2[i] = Vector2.Polar(st[i], width / 2, (zj1 - 90) * Math.PI / 180);
                    ed1[i] = Vector2.Polar(ed[i], width / 2, (zj1 + 90) * Math.PI / 180);
                    ed2[i] = Vector2.Polar(ed[i], width / 2, (zj1 - 90) * Math.PI / 180);

                    Solid sd = new Solid(st1[i], st2[i], ed1[i], ed2[i]);
                    sd.Layer = layercolor;
                    dxf.AddEntity(sd);
                }
                if (type[i] == "Arc")
                {
                    arc[i] = (Arc)poly.Explode()[i];
                    cen[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    st[i] = Vector2.Polar(cen[i], arc[i].Radius, arc[i].StartAngle * Math.PI / 180);
                    ed[i] = Vector2.Polar(cen[i], arc[i].Radius, arc[i].EndAngle * Math.PI / 180);
                    if (arc[i].StartAngle > arc[i].EndAngle) stang = arc[i].StartAngle - 360;
                    else stang = arc[i].StartAngle;

                    endang = arc[i].EndAngle;

                    for (int j = 0; j <= dfs; j++)
                    {
                        st[j] = Vector2.Polar(cen[i], arc[i].Radius, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed[j] = Vector2.Polar(cen[i], arc[i].Radius, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        st1[j] = Vector2.Polar(st[j], -width / 2, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        st2[j] = Vector2.Polar(st[j], width / 2, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed1[j] = Vector2.Polar(ed[j], -width / 2, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed2[j] = Vector2.Polar(ed[j], width / 2, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);

                        Solid sd = new Solid(st1[j], st2[j], ed1[j], ed2[j]);
                        sd.Layer = layercolor;
                        dxf.AddEntity(sd);
                    }
                }
            }
        }
        */



        public static void PolyFill(DxfDocument dxf, LwPolyline poly, double width, netDxf.Tables.Layer layercolor)  //poly类型多段线，包含线段和圆弧，设置线型和线宽
        {
            int numst1 = poly.Explode().Count;
            int numst = numst1;
            String[] type = new String[1000];
            Line[] line = new Line[1000];
            Arc[] arc = new Arc[1000];
            Vector2[] st = new Vector2[1000];
            Vector2[] ed = new Vector2[1000];
            Vector2[] st1 = new Vector2[1000];
            Vector2[] st2 = new Vector2[1000];
            Vector2[] ed1 = new Vector2[1000];
            Vector2[] ed2 = new Vector2[1000];
            double zj1;
            Vector2[] cen = new Vector2[1000];
            int dfs = 23;
            double stang, endang;

            for (int i = 0; i < numst; i++)
            {
                type[i] = poly.Explode()[i].Type.ToString();
                if (type[i] == "Line")
                {
                    line[i] = (Line)poly.Explode()[i];
                    st[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    ed[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);

                    if (ed[i].X == st[i].X)
                    {
                        if (ed[i].Y > st[i].Y) zj1 = 90;
                        else zj1 = -90;
                    }
                    else
                    {
                        zj1 = Math.Atan(Math.Abs((ed[i].Y - st[i].Y) / (ed[i].X - st[i].X))) * 180 / Math.PI;
                    }

                    if ((ed[i].X >= st[i].X) && (ed[i].Y > st[i].Y)) zj1 = zj1 * 1.0;//第一象限
                    if ((ed[i].X <= st[i].X) && (ed[i].Y > st[i].Y)) zj1 = 180.0 - zj1 * 1.0;//第二象限
                    if ((ed[i].X <= st[i].X) && (ed[i].Y < st[i].Y)) zj1 = 180.0 + zj1 * 1.0;//第三象限
                    if ((ed[i].X >= st[i].X) && (ed[i].Y < st[i].Y)) zj1 = 0.0 - zj1 * 1.0;//第四象限

                    //                   st[i] = Vector2.Polar(st[i], width / 2, (zj1+180) * Math.PI / 180);
                    //                    ed[i] = Vector2.Polar(ed[i], width / 2, zj1 * Math.PI / 180);
                    st1[i] = Vector2.Polar(st[i], width / 2, (zj1 + 90) * Math.PI / 180);
                    st2[i] = Vector2.Polar(st[i], width / 2, (zj1 - 90) * Math.PI / 180);
                    ed1[i] = Vector2.Polar(ed[i], width / 2, (zj1 + 90) * Math.PI / 180);
                    ed2[i] = Vector2.Polar(ed[i], width / 2, (zj1 - 90) * Math.PI / 180);

                    Solid sd = new Solid(st1[i], st2[i], ed1[i], ed2[i]);
                    sd.Layer = layercolor;
                    dxf.AddEntity(sd);
                    PolyFillcorner(dxf, st[i], width, layercolor);
                    PolyFillcorner(dxf, ed[i], width, layercolor);
                }
                if (type[i] == "Arc")
                {
                    arc[i] = (Arc)poly.Explode()[i];
                    cen[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    st[i] = Vector2.Polar(cen[i], arc[i].Radius, arc[i].StartAngle * Math.PI / 180);
                    ed[i] = Vector2.Polar(cen[i], arc[i].Radius, arc[i].EndAngle * Math.PI / 180);
                    if (arc[i].StartAngle > arc[i].EndAngle) stang = arc[i].StartAngle - 360;
                    else stang = arc[i].StartAngle;

                    endang = arc[i].EndAngle;

                    for (int j = 0; j <= dfs; j++)
                    {
                        st[j] = Vector2.Polar(cen[i], arc[i].Radius, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed[j] = Vector2.Polar(cen[i], arc[i].Radius, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        st1[j] = Vector2.Polar(st[j], -width / 2, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        st2[j] = Vector2.Polar(st[j], width / 2, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed1[j] = Vector2.Polar(ed[j], -width / 2, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed2[j] = Vector2.Polar(ed[j], width / 2, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);

                        Solid sd = new Solid(st1[j], st2[j], ed1[j], ed2[j]);
                        sd.Layer = layercolor;
                        dxf.AddEntity(sd);
                    }
                }
            }

        }
        /// <summary>
        /// seevector2该方法指寻找各个点的位置
        /// </summary>
        /// <param name="dxf"></param>
        /// <param name="vec2">指需要寻找点的集合</param>
        public static void seevector2(DxfDocument dxf, string starttxt, double height, int startnumber, params Vector2[] vec2)
        {
            int i = startnumber;
            foreach (Vector2 p1 in vec2)
            {
                Text txt1 = new Text(starttxt + i, p1, height, 0);
                i++;
                dxf.AddEntity(txt1);
            }
        }

        public static void PolyFillandlinetype(DxfDocument dxf, LwPolyline poly, double width, netDxf.Tables.Layer layercolor, Linetype linetype)  //poly类型多段线，包含线段和圆弧，设置线型、颜色和线宽
        {
            int numst1 = poly.Explode().Count;
            int numst = numst1;
            String[] type = new String[30];
            Line[] line = new Line[30];
            Arc[] arc = new Arc[30];
            Vector2[] st = new Vector2[30];
            Vector2[] ed = new Vector2[30];
            Vector2[] st1 = new Vector2[30];
            Vector2[] st2 = new Vector2[30];
            Vector2[] ed1 = new Vector2[30];
            Vector2[] ed2 = new Vector2[30];
            double zj1;
            Vector2[] cen = new Vector2[30];
            int dfs = 23;
            double stang, endang;

            for (int i = 0; i < numst; i++)
            {
                type[i] = poly.Explode()[i].Type.ToString();
                if (type[i] == "Line")
                {
                    line[i] = (Line)poly.Explode()[i];
                    st[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    ed[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);

                    if (ed[i].X == st[i].X)
                    {
                        if (ed[i].Y > st[i].Y) zj1 = 90;
                        else zj1 = -90;
                    }
                    else
                    {
                        zj1 = Math.Atan(Math.Abs((ed[i].Y - st[i].Y) / (ed[i].X - st[i].X))) * 180 / Math.PI;
                    }

                    if ((ed[i].X >= st[i].X) && (ed[i].Y > st[i].Y)) zj1 = zj1 * 1.0;//第一象限
                    if ((ed[i].X <= st[i].X) && (ed[i].Y > st[i].Y)) zj1 = 180.0 - zj1 * 1.0;//第二象限
                    if ((ed[i].X <= st[i].X) && (ed[i].Y < st[i].Y)) zj1 = 180.0 + zj1 * 1.0;//第三象限
                    if ((ed[i].X >= st[i].X) && (ed[i].Y < st[i].Y)) zj1 = 0.0 - zj1 * 1.0;//第四象限

                    //                   st[i] = Vector2.Polar(st[i], width / 2, (zj1+180) * Math.PI / 180);
                    //                    ed[i] = Vector2.Polar(ed[i], width / 2, zj1 * Math.PI / 180);
                    st1[i] = Vector2.Polar(st[i], width / 2, (zj1 + 90) * Math.PI / 180);
                    st2[i] = Vector2.Polar(st[i], width / 2, (zj1 - 90) * Math.PI / 180);
                    ed1[i] = Vector2.Polar(ed[i], width / 2, (zj1 + 90) * Math.PI / 180);
                    ed2[i] = Vector2.Polar(ed[i], width / 2, (zj1 - 90) * Math.PI / 180);

                    Solid sd = new Solid(st1[i], st2[i], ed1[i], ed2[i]);
                    sd.Layer = layercolor;
                    sd.Linetype = linetype;
                    dxf.AddEntity(sd);
                    PolyFillcorner(dxf, st[i], width, layercolor);
                    PolyFillcorner(dxf, ed[i], width, layercolor);
                }
                if (type[i] == "Arc")
                {
                    arc[i] = (Arc)poly.Explode()[i];
                    cen[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    st[i] = Vector2.Polar(cen[i], arc[i].Radius, arc[i].StartAngle * Math.PI / 180);
                    ed[i] = Vector2.Polar(cen[i], arc[i].Radius, arc[i].EndAngle * Math.PI / 180);
                    if (arc[i].StartAngle > arc[i].EndAngle) stang = arc[i].StartAngle - 360;
                    else stang = arc[i].StartAngle;

                    endang = arc[i].EndAngle;

                    for (int j = 0; j <= dfs; j++)
                    {
                        st[j] = Vector2.Polar(cen[i], arc[i].Radius, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed[j] = Vector2.Polar(cen[i], arc[i].Radius, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        st1[j] = Vector2.Polar(st[j], -width / 2, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        st2[j] = Vector2.Polar(st[j], width / 2, (stang + j * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed1[j] = Vector2.Polar(ed[j], -width / 2, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);
                        ed2[j] = Vector2.Polar(ed[j], width / 2, (stang + (j + 1) * (endang - stang) / (dfs + 1)) * Math.PI / 180);

                        Solid sd = new Solid(st1[j], st2[j], ed1[j], ed2[j]);
                        sd.Layer = layercolor;
                        sd.Linetype = linetype;
                        dxf.AddEntity(sd);
                    }
                }
            }
        }

        public static void PolyFillcorner(DxfDocument dxf, Vector2 pt, double width, netDxf.Tables.Layer layercolor)
        {
            int dfs = 23;
            Vector2[] st = new Vector2[30];
            Vector2[] ed = new Vector2[30];

            for (int j = 0; j <= dfs; j++)
            {
                st[j] = Vector2.Polar(pt, width / 2.0, (0 + j * 360.0 / (dfs + 1)) * Math.PI / 180);
                ed[j] = Vector2.Polar(pt, width / 2.0, (0 + (j + 1) * 360.0 / (dfs + 1)) * Math.PI / 180);

                Solid sd = new Solid(pt, st[j], ed[j]);
                sd.Layer = layercolor;
                dxf.AddEntity(sd);
            }

        }

        //autocad中的trim命令，用于剪切和延伸，使用情况和填充一样，需设置前置参数
        public static void trimandextend(DxfDocument dxf1, String[] type, int numst, Vector2 pp, double lineangle, Line[] line, Arc[] arc) //trim剪切延伸
        {
            Vector2[] startpoint = new Vector2[1000];
            Vector2[] endpoint = new Vector2[1000];

            //            Vector2[] tangentpoint = new Vector2[1000];
            Vector2[] center = new Vector2[1000];
            double[] startangle = new double[1000];
            double[] endangle = new double[1000];
            double[] radius = new double[1000];
            //            double[] radiusmax = new double[1000];
            //            double[] radiusmin = new double[1000];
            //            Ellipse[] ellipse = new Ellipse[1000];
            //            String[] type = new String[1000];

            //           double[] b = new double[1000];
            double bline = 0.0;
            double[] tt = new double[1000];
            double t = Math.Tan(lineangle * Math.PI / 180.0);
            //           double bm = -10000.0, bl = 10000.0;
            double jdx, jdy;
            double angjd = 0.0, angjd1 = 0.0;
            double aa, bb, cc, dd;
            //            int numst=0;

            //           int nl;
            //           double h = Math.Abs(pmxdist / Math.Cos(pmxangle * Math.PI / 180.0) / 8.0);
            for (int i = 0; i < numst; i++)
            {
                if (type[i] == "Line")
                {
                    startpoint[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    endpoint[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001) { tt[i] = 100000000.0; }
                    else { tt[i] = (endpoint[i].Y - startpoint[i].Y) / (endpoint[i].X - startpoint[i].X); }
                }
                if (type[i] == "Arc")
                {
                    //                    arc[i] = (Arc)poly.Explode()[i];
                    center[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    startangle[i] = arc[i].StartAngle;
                    endangle[i] = arc[i].EndAngle;
                    //                    if (startangle[i] > endangle[i]) endangle[i] = endangle[i] + 360;


                    radius[i] = arc[i].Radius;

                    startpoint[i] = Vector2.Polar(center[i], radius[i], startangle[i] * Math.PI / 180);
                    endpoint[i] = Vector2.Polar(center[i], radius[i], endangle[i] * Math.PI / 180);
                }

            }
            //           nl = (int)((bm - bl) / h);//剖面线条数
            int yxjds;//有效交点数

            Line lineyx;
            double[] xyx = new double[100];
            double[] yxy = new double[100];
            //           for (int j = 1; j <= nl; j++)
            //           {
            yxjds = 0;
            bline = pp.Y - Math.Tan(lineangle * Math.PI / 180.0) * pp.X;
            for (int i = 0; i < numst; i++)
            {
                if (type[i] == "Line")
                {
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                    {
                        jdx = startpoint[i].X;
                        jdy = bline + t * jdx;
                    }
                    else
                    {
                        jdx = (startpoint[i].Y - bline - tt[i] * startpoint[i].X) / (t - tt[i]);
                        jdy = bline + t * jdx;
                    }
                    if (distance1(new Vector2(jdx, jdy), startpoint[i]) <= distance1(startpoint[i], endpoint[i]) &&
                        distance1(new Vector2(jdx, jdy), endpoint[i]) < distance1(startpoint[i], endpoint[i]))
                    {
                        yxjds = yxjds + 1;
                        xyx[yxjds] = jdx;
                        yxy[yxjds] = jdy;
                    }
                }
                if (type[i] == "Arc")
                {
                    aa = 1 + Math.Pow(t, 2);
                    bb = -2.0 * (t * center[i].Y - bline * t + center[i].X);
                    cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bline, 2.0) - 2.0 * bline * center[i].Y - Math.Pow(radius[i], 2.0);

                    dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                    if (dd > 0)
                    {
                        jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                        jdy = bline + t * jdx;
                        if (Math.Abs(jdx - center[i].X) < 0.0000001)
                        {
                            if (jdy > center[i].Y) angjd1 = 90;
                            else angjd1 = 270;
                        }
                        //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;

                        else
                        {
                            angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                            if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                            if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                            if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                            if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                        }

                        if (startangle[i] > endangle[i])
                        {
                            if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }
                        else
                        {
                            if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }

                        jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                        jdy = bline + t * jdx;
                        if (Math.Abs(jdx - center[i].X) < 0.0000001)
                        {
                            if (jdy > center[i].Y) angjd1 = 90;
                            else angjd1 = 270;
                        }
                        //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                        else
                        {
                            angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                            if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                            if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                            if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                            if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                        }

                        if (startangle[i] > endangle[i])
                        {
                            if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }
                        else
                        {
                            if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                            {
                                yxjds = yxjds + 1;
                                xyx[yxjds] = jdx;
                                yxy[yxjds] = jdy;
                            }
                        }
                    }
                }

                //               }


                if (yxjds == 1)//有一个交点
                {
                    lineyx = new Line(new Vector2(xyx[yxjds], yxy[yxjds]), new Vector2(pp.X, pp.Y));
                    dxf1.AddEntity(lineyx);
                }


            }
        }/*
        public static Vector2 ArcandLineintersection(Vector2 p1, Vector2 p2, Vector2 arccen, Arc arc)
        {
            double k = (p1.Y - p2.Y) / (p2.X + p1.X);
            double b = p1.Y - k * p1.X;
            double r = 0;




        }
        */
        //求直线与圆弧的交点,适用于只有一个交点的情况，参数为：线段的两个端点，圆弧圆心以及圆弧
        public static Vector2 getPointForArcandLine(Vector2 p1, Vector2 p2, Vector2 arccen, Arc arc)
        {
            double a, b, c;
            double m1, m2, n1, n2;
            double x1 = p1.X, y1 = p1.Y, x2 = p2.X, y2 = p2.Y;
            Vector2 point1, point2;
            a = y2 - y1;
            b = x1 - x2;
            c = (x2 - x1) * y1 - (y2 - y1) * x1;
            if (b < 0)
            {
                a *= -1; b *= -1; c *= -1;
            }
            else if (b == 0 && a < 0)
            {
                a *= -1; c *= -1;
            }

            if (p1.Y == p2.Y)
            {
                m1 = arccen.X - Math.Sqrt(arc.Radius * arc.Radius - ((c / b) + arccen.Y) * ((c / b) + arccen.Y));
                m2 = arccen.X + Math.Sqrt(arc.Radius * arc.Radius - ((c / b) + arccen.Y) * ((c / b) + arccen.Y));
                n1 = p1.Y;
                n2 = p2.Y;
                point1 = new Vector2(m1, n1);
                point2 = new Vector2(m2, n2);
            }
            else if (p1.X == p2.X)
            {
                m1 = p1.X;
                m2 = p1.X;
                n1 = arccen.Y + Math.Sqrt(arc.Radius * arc.Radius - ((c / a) + arccen.X) * ((c / a) + arccen.X));
                n2 = arccen.Y - Math.Sqrt(arc.Radius * arc.Radius - ((c / a) + arccen.X) * ((c / a) + arccen.X));
                point1 = new Vector2(m1, n1);
                point2 = new Vector2(m2, n2);
            }
            else
            {
                double x = 1 + a * a / (b * b);
                double y = 2 * a * c / (b * b) + 2 * arccen.Y * a / b - 2 * arccen.X;
                double z = arccen.X * arccen.X + arccen.Y * arccen.Y - arc.Radius * arc.Radius + c * c / (b * b) + 2 * arccen.Y * c / b;
                m1 = (-y + Math.Sqrt(y * y - 4 * x * z)) / (2 * x);
                m2 = (-y - Math.Sqrt(y * y - 4 * x * z)) / (2 * x);
                n1 = -(c + a * m1) / b;
                n2 = -(c + a * m2) / b;
                point1 = new Vector2(m1, n1);
                point2 = new Vector2(m2, n2);
            }


            Vector2 point = new Vector2(0, 0);
            if (arc.StartAngle > 90 && arc.EndAngle < 270)
            {
                point = point1;
            }
            else
            {
                point = point2;
            }

            return point;

        }

        //标注，
        public static void dxflinedim(DxfDocument dxf, LinearDimension dim, double prarm)  //标注
        {
            dim.UserText = String.Format("{0}", prarm);
            dxf.AddEntity(dim);
        }
        /// <summary>
        /// LineArcI中89.9°相当于90°
        /// </summary>
        /// <param name="arc"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns>
        //圆与直线的交点,当直线与x轴垂直时，数组中第一个是y值较小的那个,否则，数组中第一个是x值较小的那个。
        public static Vector2[] LineArcI(Arc arc, Vector2 p1, Vector2 p2)
        {
            double pi = Math.PI;
            double x1 = 0;
            double x2 = 0;
            double y1 = 0;
            double y2 = 0;
            double h = arc.Center.X;
            double v = arc.Center.Y;
            double radius = arc.Radius;
            double k = 0;
            //控制斜率
            if (p1.X != p2.X)
            {
                k = (p1.Y - p2.Y) / (p1.X - p2.X);
                if (k > Math.Tan(pi / 180 * 89.99) || k < Math.Tan(-pi / 180 * 89.99))
                    p1.X = p2.X;
            }
            //控制斜率
            //斜率不存在x=x1或者x=x2
            if (p1.X == p2.X)
            {
                x1 = x2 = p1.X;
                double t = radius * radius - (x1 - h) * (x1 - h);
                if (t < 0)
                {
                    System.Diagnostics.Debug.WriteLine(555555);//打印
                    return null;
                }
                else if (t > 0)
                {
                    y1 = v - Math.Sqrt(t);
                    y2 = v + Math.Sqrt(t);
                    return new Vector2[] { new Vector2(x1, y1), new Vector2(x1, y2) };
                }
                else
                    return new Vector2[] { new Vector2(x1, v) };
            }
            //斜率存在即y=kx+b;
            else
            {
                //斜率
                double b = p1.Y - k * p1.X;
                double A = 1 + k * k;
                double B = 2 * k * (b - v) - 2 * h;
                double C = h * h + (b - v) * (b - v) - radius * radius;
                double delta = B * B - 4 * A * C;
                if (delta < 0)
                {
                    return null;
                }
                else if (delta > 0)
                {
                    x1 = (-B - Math.Sqrt(delta)) / (2 * A);
                    y1 = k * x1 + b;
                    x2 = (-B + Math.Sqrt(delta)) / (2 * A);
                    y2 = k * x2 + b;
                    return new Vector2[] { new Vector2(x1, y1), new Vector2(x2, y2) };
                }
                else
                {
                    x1 = -B / (2 * A);
                    y1 = k * x1 + b;
                    return new Vector2[] { new Vector2(x1, y1) };
                }

            }


        }
        public static void dxflinedim(DxfDocument dxf, LinearDimension dim, string prarm)  //标注
        {
            dim.UserText = String.Format("{0}", prarm);
            dxf.AddEntity(dim);
        }

        public static void dxfangdim(DxfDocument dxf, Angular2LineDimension dim, double prarm)  //角度标注
        {
            dim.UserText = String.Format("{0}", prarm);
            dxf.AddEntity(dim);
        }

        public static void jdbz1(DxfDocument dxf, Line line1, Line line2, double offset, String zh, double height)  //角度标注
        {
            double pi = Math.PI;
            DimensionStyle dimensionStyle = new DimensionStyle("ds");
            dimensionStyle.ArrowSize = 3.5;
            dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(new Vector2(line1.StartPoint.X, line1.StartPoint.Y), new Vector2(line1.EndPoint.X, line1.EndPoint.Y)), new Line(new Vector2(line2.StartPoint.X, line2.StartPoint.Y), new Vector2(line2.EndPoint.X, line2.EndPoint.Y)), offset, dimensionStyle);
            angdim1.UserText = "  ";

            dxf.AddEntity(angdim1);

            TextStyle style = new TextStyle("E:\\ikun\\simsun.ttf");
            Vector2 p5 = new Vector2(line1.StartPoint.X, line1.StartPoint.Y);
            Vector2 p11 = new Vector2(line1.EndPoint.X, line1.EndPoint.Y);
            Vector2 p6 = new Vector2(line2.StartPoint.X, line2.StartPoint.Y);
            Vector2 p12 = new Vector2(line2.EndPoint.X, line2.EndPoint.Y);
            Vector2 p0 = fun.Inters(p5, p11, p6, p12);


            int l = zh.Length;

            double angle_1 = fun.CalAngle(p5, p11);
            double angle_2 = fun.CalAngle(p6, p12);

            Console.WriteLine("12331231");
            if (Math.Abs(angle_2 - angle_1) <= pi)
            {
                Vector2 p01 = fun.Polar(p0, (angle_2 - angle_1) / 2 + angle_1, offset);
                double angle_3 = fun.CalAngle(p0, p01);
                Console.WriteLine("1");
                if (angle_3 > 90 * pi / 180 && angle_3 < 270 * pi / 180)
                {
                    Text texe1 = new Text(zh, fun.Polar(p01, pi, l * height * 0.9), height, 0, style);
                    dxf.AddEntity(texe1);
                }

                else
                {
                    Text texe1 = new Text(zh, p01, height, 0, style);
                    dxf.AddEntity(texe1);
                }
                //Vector2 p01 = fun.Polar(p0, (angle_2 - angle_1) / 2 + angle_1, offset);
                //Text texe1 = new Text(zh, p01, height, 0, style);
                //dxf.AddEntity(texe1);
            }

            else
            {
                Console.WriteLine(angle_1);
                Console.WriteLine(angle_2);
                Console.WriteLine(pi);

                if (angle_2 <= pi && angle_1 > pi)
                {
                    Vector2 p01 = fun.Polar(p0, (pi * 2 - Math.Abs(angle_2 - angle_1)) / 2 + angle_1, offset);
                    double angle_3 = fun.CalAngle(p0, p01);
                    if (angle_3 > 90 * pi / 180 && angle_3 < 270 * pi / 180)
                    {
                        Text texe1 = new Text(zh, fun.Polar(p01, pi, l * height * 0.9), height, 0, style);
                        dxf.AddEntity(texe1);
                    }

                    else
                    {
                        Text texe1 = new Text(zh, p01, height, 0, style);
                        dxf.AddEntity(texe1);
                    }
                    //Vector2 p01 = fun.Polar(p0, Math.Abs(angle_1) + (pi * 2 - (Math.Abs(angle_2 - angle_1))) / 2, offset);
                    //Text texe2 = new Text(zh, fun.Polar(p01, pi, l * height * 0.9), height, 0, style);
                    //dxf.AddEntity(texe2);
                }

                else if (angle_1 <= pi && angle_2 <= pi)
                {
                    Vector2 p01 = fun.Polar(p0, (angle_2 - angle_1) / 2 + angle_1, offset);
                    double angle_3 = fun.CalAngle(p0, p01);
                    if (angle_3 > 90 * pi / 180 && angle_3 < 270 * pi / 180)
                    {
                        Text texe1 = new Text(zh, fun.Polar(p01, pi, l * height * 0.9), height, 0, style);
                        dxf.AddEntity(texe1);
                    }

                    else
                    {
                        Text texe1 = new Text(zh, p01, height, 0, style);
                        dxf.AddEntity(texe1);
                    }

                }
                else
                {
                    Vector2 p01 = fun.Polar(p0, (pi * 2 - Math.Abs(angle_2 - angle_1)) / 2 + angle_2, offset);
                    Text texe1 = new Text(zh, p01, height, 0, style);
                    dxf.AddEntity(texe1);
                }
            }
        }
        public static void jdbz2(DxfDocument dxf, Line line1, Line line2, double offset, double height, double arrowsize1, double exline, String text, String toh, String tol)  //角度标注
        {
            double pi = Math.PI;
            double arrowsize = 3.5;
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = arrowsize;
            dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(new Vector2(line1.StartPoint.X, line1.StartPoint.Y), new Vector2(line1.EndPoint.X, line1.EndPoint.Y)), new Line(new Vector2(line2.StartPoint.X, line2.StartPoint.Y), new Vector2(line2.EndPoint.X, line2.EndPoint.Y)), offset, dimensionStyle);
            angdim1.UserText = "  ";

            //dxf.AddEntity(angdim1);

            TextStyle style = new TextStyle("E:\\ikun\\simsun.ttf");
            Vector2 p5 = new Vector2(line1.StartPoint.X, line1.StartPoint.Y);
            Vector2 p11 = new Vector2(line1.EndPoint.X, line1.EndPoint.Y);
            Vector2 p6 = new Vector2(line2.StartPoint.X, line2.StartPoint.Y);
            Vector2 p12 = new Vector2(line2.EndPoint.X, line2.EndPoint.Y);
            Vector2 p0 = fun.Inters(p5, p11, p6, p12);



            var line3 = new Line(p5, fun.Polar(p0, fun.CalAngle(p11, p5), -(offset + exline)));
            var line4 = new Line(p6, fun.Polar(p0, fun.CalAngle(p12, p6), -(offset + exline)));

            double ang;
            if (fun.CalAngle(p6, p12) < fun.CalAngle(p5, p11))
            {
                ang = fun.CalAngle(p6, p12) + 360 * pi / 180;
            }
            else
            {
                ang = fun.CalAngle(p6, p12);
            }

            LwPolyline Arc00 = new LwPolyline();
            Arc00.Vertexes.Add(new LwPolylineVertex(fun.Polar(p0, fun.CalAngle(p11, p5), -offset)));
            Arc00.Vertexes.Add(new LwPolylineVertex(fun.Polar(p0, fun.CalAngle(p12, p6), -offset)));
            Arc00.Vertexes[0].Bulge = -Math.Tan((fun.CalAngle(p5, p11) - ang) / 4);


            var r = arrowsize;

            if (fun.CalDistance(fun.Polar(p0, fun.CalAngle(p11, p5), -offset), fun.Polar(p0, fun.CalAngle(p12, p6), -offset)) <= height)
            {
                var a1 = fun.Polar(p0, fun.CalAngle(p11, p5), -offset);
                var a2 = fun.Polar(a1, fun.CalAngle(p11, p5) - 80 * pi / 180, r / (-Math.Cos(10 * pi / 180)));
                var a3 = fun.Polar(a1, fun.CalAngle(p11, p5) - 100 * pi / 180, r / (-Math.Cos(10 * pi / 180)));

                var a4 = fun.Polar(p0, fun.CalAngle(p12, p6), -offset);
                var a5 = fun.Polar(a4, fun.CalAngle(p12, p6) + 80 * pi / 180, r / (-Math.Cos(10 * pi / 180)));
                var a6 = fun.Polar(a4, fun.CalAngle(p12, p6) + 100 * pi / 180, r / (-Math.Cos(10 * pi / 180)));

                Solid solid1 = new Solid(a1, a2, a3)
                {
                    Layer = Layer.Default,
                    Linetype = Linetype.ByBlock,
                    Color = AciColor.ByBlock
                };
                Solid solid2 = new Solid(a4, a5, a6)
                {
                    Layer = Layer.Default,
                    Linetype = Linetype.ByBlock,
                    Color = AciColor.ByBlock
                };

                dxf.AddEntity(solid1);
                dxf.AddEntity(solid2);

                var line5 = new Line(a1, fun.Polar(a1, fun.CalAngle(p11, p5) - 90 * pi / 180, -r * 2));
                var line6 = new Line(a4, fun.Polar(a4, fun.CalAngle(p12, p6) + 90 * pi / 180, -r * 2));


                dxf.AddEntity(line5);
                dxf.AddEntity(line6);

            }
            else
            {
                var a1 = fun.Polar(p0, fun.CalAngle(p11, p5), -offset);
                var a2 = fun.Polar(a1, fun.CalAngle(p11, p5) - 70 * pi / 180, -r / (-Math.Cos(10 * pi / 180)));
                var a3 = fun.Polar(a1, fun.CalAngle(p11, p5) - 90 * pi / 180, -r / (-Math.Cos(10 * pi / 180)));

                var a4 = fun.Polar(p0, fun.CalAngle(p12, p6), -offset);
                var a5 = fun.Polar(a4, fun.CalAngle(p12, p6) + 70 * pi / 180, -r / (-Math.Cos(10 * pi / 180)));
                var a6 = fun.Polar(a4, fun.CalAngle(p12, p6) + 90 * pi / 180, -r / (-Math.Cos(10 * pi / 180)));

                Solid solid1 = new Solid(a1, a2, a3)
                {
                    Layer = Layer.Default,
                    Linetype = Linetype.ByBlock,
                    Color = AciColor.ByBlock
                };
                Solid solid2 = new Solid(a4, a5, a6)
                {
                    Layer = Layer.Default,
                    Linetype = Linetype.ByBlock,
                    Color = AciColor.ByBlock
                };

                dxf.AddEntity(solid1);
                dxf.AddEntity(solid2);
            }



            dxf.AddEntity(line3);
            dxf.AddEntity(line4);
            dxf.AddEntity(Arc00);

            int l = text.Length;
            int l1 = toh.Length;
            int l2 = tol.Length;
            var L = l * height * 0.7;

            double h;
            if (l1 >= l2)
            {
                h = l * height * 0.7 + l1 * height * 0.5 * 0.7;
            }
            else
            {
                h = l * height * 0.7 + l2 * height * 0.5 * 0.7;
            }

            double angle_1 = fun.CalAngle(p5, p11);
            double angle_2 = fun.CalAngle(p6, p12);

            Console.WriteLine("12331231");
            if (Math.Abs(angle_2 - angle_1) <= pi)
            {
                Vector2 p01 = fun.Polar(p0, (angle_2 - angle_1) / 2 + angle_1, offset);
                double angle_3 = fun.CalAngle(p0, p01);
                Console.WriteLine("1");
                if (angle_3 > 90 * pi / 180 && angle_3 < 270 * pi / 180)
                {
                    Text texe1 = new Text(text, fun.Polar(p01, pi, h), height, 0, style);
                    dxf.AddEntity(texe1);

                    Text texe2 = new Text(toh, fun.Polar(fun.Polar(fun.Polar(p01, pi, h), 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                    dxf.AddEntity(texe2);

                    Text texe3 = new Text(tol, fun.Polar(fun.Polar(p01, pi, h), 0, L), height * 0.5, 0, style);
                    dxf.AddEntity(texe3);
                }

                else
                {
                    Text texe1 = new Text(text, p01, height, 0, style);
                    dxf.AddEntity(texe1);

                    Text texe2 = new Text(toh, fun.Polar(fun.Polar(p01, 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                    dxf.AddEntity(texe2);

                    Text texe3 = new Text(tol, fun.Polar(p01, 0, L), height * 0.5, 0, style);
                    dxf.AddEntity(texe3);
                }
                //Vector2 p01 = fun.Polar(p0, (angle_2 - angle_1) / 2 + angle_1, offset);
                //Text texe1 = new Text(zh, p01, height, 0, style);
                //dxf.AddEntity(texe1);
            }

            else
            {
                Console.WriteLine(angle_1);
                Console.WriteLine(angle_2);
                Console.WriteLine(pi);

                if (angle_2 <= pi && angle_1 > pi && angle_2 != 0)
                {
                    Vector2 p01 = fun.Polar(p0, (pi * 2 - Math.Abs(angle_2 - angle_1)) / 2 + angle_1, offset);
                    double angle_3 = fun.CalAngle(p0, p01);
                    if (angle_3 > 90 * pi / 180 && angle_3 < 270 * pi / 180)
                    {
                        Text texe1 = new Text(text, fun.Polar(p01, pi, h), height, 0, style);
                        dxf.AddEntity(texe1);

                        Text texe2 = new Text(toh, fun.Polar(fun.Polar(fun.Polar(p01, pi, h), 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                        dxf.AddEntity(texe2);

                        Text texe3 = new Text(tol, fun.Polar(fun.Polar(p01, pi, h), 0, L), height * 0.5, 0, style);
                        dxf.AddEntity(texe3);
                    }

                    else
                    {
                        Text texe1 = new Text(text, p01, height, 0, style);
                        dxf.AddEntity(texe1);

                        Text texe2 = new Text(toh, fun.Polar(fun.Polar(p01, 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                        dxf.AddEntity(texe2);

                        Text texe3 = new Text(tol, fun.Polar(p01, 0, L), height * 0.5, 0, style);
                        dxf.AddEntity(texe3);
                    }
                    //Vector2 p01 = fun.Polar(p0, Math.Abs(angle_1) + (pi * 2 - (Math.Abs(angle_2 - angle_1))) / 2, offset);
                    //Text texe2 = new Text(zh, fun.Polar(p01, pi, l * height * 0.9), height, 0, style);
                    //dxf.AddEntity(texe2);
                }

                if (angle_1 <= pi && angle_2 <= pi)
                {
                    Vector2 p01 = fun.Polar(p0, (angle_2 - angle_1) / 2 + angle_1, offset);
                    double angle_3 = fun.CalAngle(p0, p01);
                    if (angle_3 > 90 * pi / 180 && angle_3 < 270 * pi / 180)
                    {
                        Text texe1 = new Text(text, fun.Polar(p01, pi, h), height, 0, style);
                        dxf.AddEntity(texe1);

                        Text texe2 = new Text(toh, fun.Polar(fun.Polar(fun.Polar(p01, pi, h), 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                        dxf.AddEntity(texe2);

                        Text texe3 = new Text(tol, fun.Polar(fun.Polar(p01, pi, h), 0, L), height * 0.5, 0, style);
                        dxf.AddEntity(texe3);
                    }

                    else
                    {
                        Text texe1 = new Text(text, p01, height, 0, style);
                        dxf.AddEntity(texe1);

                        Text texe2 = new Text(toh, fun.Polar(fun.Polar(p01, 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                        dxf.AddEntity(texe2);

                        Text texe3 = new Text(tol, fun.Polar(p01, 0, L), height * 0.5, 0, style);
                        dxf.AddEntity(texe3);
                    }

                }
                else if (fun.CalAngle(p6, p12) == 0)
                {
                    Vector2 p01 = fun.Polar(p0, -((pi * 2 - Math.Abs(angle_2 - angle_1))) / 2, offset);
                    Text texe1 = new Text(text, p01, height, 0, style);
                    dxf.AddEntity(texe1);

                    Text texe2 = new Text(toh, fun.Polar(fun.Polar(p01, 0, L), 90 * pi / 180, height * 0.5), height * 0.5, 0, style);
                    dxf.AddEntity(texe2);

                    Text texe3 = new Text(tol, fun.Polar(p01, 0, L), height * 0.5, 0, style);
                    dxf.AddEntity(texe3);
                }
            }
        }


        public static Arc DrawArc4(Vector2 v1, double radius, Vector2 v3, double Judgment) //三点画圆弧（起点，半径、终点,判断点0或非0<0是劣弧，1是优弧>）
        {


            var a = v1.X - v3.X;
            var b = v1.Y - v3.Y;

            //  var e = ((v1.X * v1.X - v3.X * v3.X) + (v1.Y * v1.Y - v2.Y * v2.Y)) / 2.0;
            //  var f = ((v1.X * v1.X - v3.X * v3.X) + (v1.Y * v1.Y - v3.Y * v3.Y)) / 2.0;

            var det = Math.Sqrt(a * a + b * b);
            double ang1 = CalAngle(v1, v3);
            Vector2 x0 = fun.Polar(v1, ang1, det / 2);
            double c = Math.Sqrt(((radius * radius) - (det / 2.0 * det / 2.0)));
            System.Diagnostics.Debug.WriteLine(c);
            Vector2 v0;
            if (Judgment == 0)
            {
                v0 = fun.Polar(x0, ang1 + Math.PI / 2, c);
            }
            else
            {
                v0 = fun.Polar(x0, ang1 - Math.PI / 2, c);

            }





            Arc arc = fun.DrawArc(v1, v0, v3);


            return arc;
        }





        public static Arc DrawArc5(Vector2 v1, Line line1, Vector2 v3) //注意事项
                                                                       //需要不与x轴平行的直线做的话需要加if
                                                                       //弧线两个点不能与相切的那条线重合
        {
            Arc arc;
            //if (line1.StartPoint.X == 0 && line1.EndPoint.X == 0)
            //{
            double l = line1.StartPoint.Y;
            double l1 = line1.EndPoint.Y;
            double l2 = line1.StartPoint.X;
            double l3 = line1.EndPoint.X;
            double k = (l1 - l) / (l3 - l2);
            double b1 = -k * l2 + l;
            Arc arc1 = fun.DrawArc(v1, fun.Polar(v1, fun.CalAngle(v1, v3), fun.CalDistance(v1, v3) / 2.0), v3);
            var r = arc1.Radius;
            Vector3 p1 = line1.StartPoint;
            Vector2 p2;
            Vector2 p3;
            Vector2 p4;
            Vector2 p5;
            double d = 0;
            double r1 = 0;
            double d1 = 0;
            var i = r;
            if (l == l1 || l2 == l3)
            {
                do
                {
                    var a = arc1.Center.X;
                    var z = Math.Sqrt((i * i) - (Math.Pow((fun.CalDistance(v1, v3) / 2.0), 2)));
                    p2 = fun.Polar(v1, fun.CalAngle(v1, v3), fun.CalDistance(v1, v3) / 2.0);
                    p3 = fun.Polar(p2, fun.CalAngle(v1, v3) + Math.PI / 2, z);
                    Arc arc2 = fun.DrawArc(v1, p3, v3);
                    var a1 = arc2.Center.X;
                    var b = arc2.Center.Y;
                    r1 = arc2.Radius;

                    d1 = ((2 * a) * (2 * a)) - 4 * 1 * ((a * a) + Math.Pow((l - b), 2) - (r * r));
                    /*    System.Diagnostics.Debug.WriteLine(d );//6.25
                         System.Diagnostics.Debug.WriteLine(d1);//-56.25
                         System.Diagnostics.Debug.WriteLine(r1 );//6.25
                         System.Diagnostics.Debug.WriteLine(r);//5
                         System.Diagnostics.Debug.WriteLine(66666665);
                         System.Diagnostics.Debug.WriteLine(66666665);*/
                    if (l == l1)
                    {
                        d = Math.Abs(b - l);
                        if (d > r1)
                            break;
                    }
                    if (l2 == l3)
                    {
                        d = Math.Abs(a1 - l2);
                        if (d > r1)
                            break;
                    }
                    i += 0.01;
                } while (i < 500);
            }
            else
            {
                do
                {
                    var a = arc1.Center.X;
                    var z = Math.Sqrt((i * i) - (Math.Pow((fun.CalDistance(v1, v3) / 2.0), 2)));
                    p2 = fun.Polar(v1, fun.CalAngle(v1, v3), fun.CalDistance(v1, v3) / 2.0);
                    p3 = fun.Polar(p2, fun.CalAngle(v1, v3) + Math.PI / 2, z);
                    Arc arc2 = fun.DrawArc(v1, p3, v3);
                    var a1 = arc2.Center.X;
                    var b = arc2.Center.Y;
                    r1 = arc2.Radius;
                    var va = 1 + (k * k);
                    var vb = -2 * a1 + 2 * k * b1 - 2 * b * k;
                    var vc = (a1 * a1) + (b1 * b1) - 2 * b * b1 + (b * b) - (r1 * r1);
                    d = (vb * vb) - 4 * va * vc;
                    i += 0.01;
                    if (d < 0)
                        break;
                } while (i < 500);
            }
            p4 = fun.Polar(v1, fun.CalAngle(v1, v3), fun.CalDistance(v1, v3) / 2.0);
            p5 = fun.Polar(p4, fun.CalAngle(v1, v3) + Math.PI / 2, Math.Sqrt((Math.Pow(r1, 2)) - (Math.Pow((fun.CalDistance(v1, v3) / 2.0), 2))));
            arc = fun.DrawArc(v1, p5, v3);
            return arc;
        }

        /// 以中心点旋转Angle角度
        /// </summary>
        /// <param name="center”>中心点</param>
        /// <param name="p1”>待旋转的点</param>
        // <param name="angle">旋转角度(弧度) </param>
        public static Vector2 PointRotate(Vector2 center, Vector2 p1, double angle)//顺时针旋转
        {
            //var x1=(p)
            double x1 = (p1.X - center.X) * Math.Cos(angle) + (p1.Y - center.Y) * Math.Sin(angle) + center.X;
            double y1 = -(p1.X - center.X) * Math.Sin(angle) + (p1.Y - center.Y) * Math.Cos(angle) + center.Y;
            p1.X = (int)x1;
            p1.Y = (int)y1;
            var r1 = new Vector2(x1, y1);

            return r1;

        }
        /// 以中心点center阵列x个
        /// </summary>
        /// <param name="lwPolyline”>待旋转的多段线</param>
        // <param name="x">阵列后的个数 </param>
        public static void Circulararray1(DxfDocument dxf, LwPolyline lwPolyline, double x, double width, netDxf.Tables.Layer layercolor)  //绕圆阵列
        {
            LwPolyline[] lp = new LwPolyline[1000];
            int sy = 0;
            for (int i = 0; i < x; i++)
            {

                lp[sy] = Pus.RotateByOrigin((360 / x) * (i + 1) * Math.PI / 180, lwPolyline);
                lp[sy].Layer = lwPolyline.Layer;
                PolyFill(dxf, lp[i], width, layercolor);
                sy++;

            }
        }

        public static void drawcircle(DxfDocument dxf, Vector2 circen, double r, double width, netDxf.Tables.Layer layercolor)  //有线宽的圆 circen：圆心意思
        {
            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(fun.Polar(circen, 0, r)));
            poly.Vertexes[0].Bulge = Math.Tan(Math.PI / 4);
            poly.Vertexes.Add(new LwPolylineVertex(fun.Polar(circen, Math.PI, r)));
            poly.Vertexes[1].Bulge = Math.Tan(Math.PI / 4);
            poly.Vertexes.Add(new LwPolylineVertex(fun.Polar(circen, 0, r)));
            PolyFill(dxf, poly, width, layercolor);
        }

        public static void hatchsuboflineandarc(DxfDocument dxf1, String[] type, int numst, double pmxangle, double pmxdist, Line[] line, Arc[] arc, netDxf.Tables.Layer layercolor)  //有圆弧的填充
        {
            //            Line[] line = new Line[1000];
            //            Arc[] arc = new Arc[1000];
            //            Circle[] circle = new Circle[1000];
            Vector2[] startpoint = new Vector2[1000];
            Vector2[] endpoint = new Vector2[1000];

            Vector2[] tangentpoint = new Vector2[1000];
            Vector2[] center = new Vector2[1000];
            double[] startangle = new double[1000];
            double[] endangle = new double[1000];
            double[] radius = new double[1000];
            double[] radiusmax = new double[1000];
            double[] radiusmin = new double[1000];
            //            Ellipse[] ellipse = new Ellipse[1000];
            //            String[] type = new String[1000];

            double[] b = new double[1000];
            double[] bpmx = new double[1000];
            double[] tt = new double[1000];
            double t = Math.Tan(pmxangle * Math.PI / 180.0);
            double bm = -10000.0, bl = 10000.0;
            double jdx, jdy;
            double angjd = 0.0, angjd1 = 0.0;
            double aa, bb, cc, dd;
            //            int numst=0;
            double tangentangle;
            double tangentangle1;

            int nl;
            double h = Math.Abs(pmxdist / Math.Cos(pmxangle * Math.PI / 180.0) / 8.0);
            for (int i = 0; i < numst; i++)
            {
                if (type[i] == "Line")
                {
                    startpoint[i] = new Vector2(line[i].StartPoint.X, line[i].StartPoint.Y);
                    endpoint[i] = new Vector2(line[i].EndPoint.X, line[i].EndPoint.Y);
                    if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001) { tt[i] = 100000000.0; }
                    else { tt[i] = (endpoint[i].Y - startpoint[i].Y) / (endpoint[i].X - startpoint[i].X); }
                    b[i] = startpoint[i].Y - t * startpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];
                }
                if (type[i] == "Arc")
                {
                    //                    arc[i] = (Arc)poly.Explode()[i];
                    center[i] = new Vector2(arc[i].Center.X, arc[i].Center.Y);
                    startangle[i] = arc[i].StartAngle;
                    endangle[i] = arc[i].EndAngle;
                    //                    if (startangle[i] > endangle[i]) endangle[i] = endangle[i] + 360;


                    radius[i] = arc[i].Radius;

                    startpoint[i] = Vector2.Polar(center[i], radius[i], startangle[i] * Math.PI / 180);
                    endpoint[i] = Vector2.Polar(center[i], radius[i], endangle[i] * Math.PI / 180);

                    tangentangle = pmxangle + 90;

                    if (tangentangle >= startangle[i] && tangentangle <= endangle[i]) tangentangle1 = tangentangle;
                    else tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);


                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];

                    tangentangle = pmxangle - 90;

                    if (tangentangle >= startangle[i] && tangentangle <= endangle[i]) tangentangle1 = tangentangle;
                    else tangentangle1 = tangentangle - 180;

                    tangentpoint[i] = Vector2.Polar(center[i], radius[i], tangentangle1 * Math.PI / 180);


                    b[i] = tangentpoint[i].Y - t * tangentpoint[i].X;
                    if (b[i] > bm) bm = b[i];
                    if (b[i] < bl) bl = b[i];


                }


            }
            nl = (int)((bm - bl) / h);//剖面线条数
            int yxjds;//有效交点数

            Line[] lineyx = new Line[100];
            double[] xyx = new double[100];
            double[] yxy = new double[100];
            for (int j = 1; j < nl; j++)
            {
                yxjds = 0;
                bpmx[j] = bl + h * j;
                for (int i = 0; i < numst; i++)
                {
                    if (type[i] == "Line")
                    {
                        if (Math.Abs(endpoint[i].X - startpoint[i].X) < 0.00000001)
                        {
                            jdx = startpoint[i].X;
                            jdy = bpmx[j] + t * jdx;
                        }
                        else
                        {
                            jdx = (startpoint[i].Y - bpmx[j] - tt[i] * startpoint[i].X) / (t - tt[i]);
                            jdy = bpmx[j] + t * jdx;
                        }
                        if (distance1(new Vector2(jdx, jdy), startpoint[i]) <= distance1(startpoint[i], endpoint[i]) &&
                            distance1(new Vector2(jdx, jdy), endpoint[i]) < distance1(startpoint[i], endpoint[i]))
                        {
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }
                    if (type[i] == "Arc")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd1 = 90;
                                else angjd1 = 270;
                            }
                            //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;

                            else
                            {
                                angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                            }

                            if (startangle[i] > endangle[i])
                            {
                                if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            else
                            {
                                if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            if (Math.Abs(jdx - center[i].X) < 0.0000001)
                            {
                                if (jdy > center[i].Y) angjd1 = 90;
                                else angjd1 = 270;
                            }
                            //                            else angjd = Math.Atan2(jdy - center[i].Y, jdx - center[i].X) * 180 / Math.PI;
                            else
                            {
                                angjd = Math.Atan(Math.Abs((jdy - center[i].Y) / (jdx - center[i].X))) * 180 / Math.PI;
                                if (jdy >= center[i].Y && jdx > center[i].X) angjd1 = angjd;
                                if (jdy >= center[i].Y && jdx < center[i].X) angjd1 = 180 - angjd;
                                if (jdy <= center[i].Y && jdx < center[i].X) angjd1 = 180 + angjd;
                                if (jdy <= center[i].Y && jdx > center[i].X) angjd1 = 360 - angjd;
                            }

                            if (startangle[i] > endangle[i])
                            {
                                if ((angjd1 <= 360 && angjd1 >= startangle[i]) || (angjd1 >= 0 && angjd1 <= endangle[i]))
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                            else
                            {
                                if (angjd1 <= endangle[i] && angjd1 >= startangle[i])
                                {
                                    yxjds = yxjds + 1;
                                    xyx[yxjds] = jdx;
                                    yxy[yxjds] = jdy;
                                }
                            }
                        }
                    }
                    if (type[i] == "Circle")
                    {
                        aa = 1 + Math.Pow(t, 2);
                        bb = -2.0 * (t * center[i].Y - bpmx[j] * t + center[i].X);
                        cc = Math.Pow(center[i].X, 2.0) + Math.Pow(center[i].Y, 2.0) + Math.Pow(bpmx[j], 2.0) - 2.0 * bpmx[j] * center[i].Y - Math.Pow(radius[i], 2.0);

                        dd = Math.Pow(bb, 2.0) - 4.0 * aa * cc;
                        if (dd > 0)
                        {
                            jdx = (-bb + Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;


                            jdx = (-bb - Math.Sqrt(Math.Pow(bb, 2.0) - 4.0 * aa * cc)) / (2.0 * aa);
                            jdy = bpmx[j] + t * jdx;
                            yxjds = yxjds + 1;
                            xyx[yxjds] = jdx;
                            yxy[yxjds] = jdy;
                        }
                    }

                }

                //重新排序
                double tempx, tempy;
                for (int jj = 1; jj < yxjds; jj++)
                {
                    for (int ii = 1; ii < yxjds - 1; ii++)
                        if (xyx[ii + 1] < xyx[ii])
                        {
                            tempx = xyx[ii];
                            tempy = yxy[ii];
                            xyx[ii] = xyx[ii + 1];
                            yxy[ii] = yxy[ii + 1];
                            xyx[ii + 1] = tempx;
                            yxy[ii + 1] = tempy;
                        }
                }
                if ((int)(yxjds / 2) == (yxjds / 2))    //偶数个交点
                {
                    for (int kk = 1; kk < yxjds / 2; kk++)
                    {
                        lineyx[kk] = new Line(new Vector2(xyx[kk * 2 - 1], yxy[kk * 2 - 1]), new Vector2(xyx[kk * 2], yxy[kk * 2]));
                        lineyx[kk].Layer = layercolor;
                        dxf1.AddEntity(lineyx[kk]);
                    }
                }

                //               int bjz1 = 10*(int)(yxjds / 2);
                //               int bjz2 = (int)(10.0*yxjds / 2.0);



            }
        }

        public static void ellipse1(DxfDocument dxf, double r_max, double r_min, Vector2 center, double angle) //新椭圆
        {
            double pi = Math.PI;
            Vector2 r_min_up = new Vector2(center.X, center.Y + r_min);
            Vector2 r_min_down = new Vector2(center.X, center.Y - r_min);
            Vector2 r_max_left = new Vector2(center.X - r_max, center.Y);
            Vector2 r_max_right = new Vector2(center.X + r_max, center.Y);
            Vector2 vr_min = fun.getPointForArcandLine(center, fun.Polar(center, angle * pi / 180, r_max * 2), center, new Arc(center, r_min, 0, 360));//小圆上的点
            Vector2 vr_max = fun.getPointForArcandLine(center, fun.Polar(center, angle * pi / 180, r_max * 2), center, new Arc(center, r_min, 0, 360));//大圆上的点
            Vector2 v_insert = fun.Inters(vr_max, fun.Polar(vr_max, -90 * pi / 180, r_max), vr_min, fun.Polar(vr_min, 180 * pi / 180, r_max));//大圆和小圆延长线在椭圆上的交点（菱形中点）
            Vector2 v_insert_right = fun.mirrorpoint(v_insert, r_min_up, r_min_down);//上半右侧交点
            Vector2 v_insert_right_down = fun.mirrorpoint(v_insert_right, r_max_left, r_max_right);//下半右侧交点
            Vector2 v_insert_down = fun.mirrorpoint(v_insert, r_max_left, r_max_right);//下半左侧交点
            var a1 = fun.DrawArc3(v_insert, r_max_left, v_insert_down);
            var a2 = fun.DrawArc3(v_insert_right, r_min_up, v_insert);
            var a3 = fun.DrawArc3(v_insert_right_down, r_max_right, v_insert_right);
            var a4 = fun.DrawArc3(v_insert_down, r_min_down, v_insert_right_down);
            dxf.AddEntity(a1);
            dxf.AddEntity(a2);
            dxf.AddEntity(a3);
            dxf.AddEntity(a4);

        }

        private static Vector2 bezier3func(double uu, Vector2[] pp)  //3次贝塞尔曲线 
        {
            Vector2 part0 = new Vector2(pp[0].X * uu * uu * uu, pp[0].Y * uu * uu * uu);
            Vector2 part1 = new Vector2(pp[1].X * 3 * uu * uu * (1 - uu), pp[1].Y * 3 * uu * uu * (1 - uu));
            Vector2 part2 = new Vector2(pp[2].X * 3 * uu * (1 - uu) * (1 - uu), pp[2].Y * 3 * uu * (1 - uu) * (1 - uu));
            Vector2 part3 = new Vector2(pp[3].X * (1 - uu) * (1 - uu) * (1 - uu), pp[3].Y * (1 - uu) * (1 - uu) * (1 - uu));
            return new Vector2(part0.X + part1.X + part2.X + part3.X, part0.Y + part1.Y + part2.Y + part3.Y);
        }

        public static void WriteSpline(DxfDocument dxf, Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)  //四点作为控制点的三次贝塞尔曲线
        {

            List<SplineVertex> ctrlPoints = new List<SplineVertex>//4个控制点构造一个三次贝塞尔曲线
            {
                new SplineVertex(Tothree(p1)),
                new SplineVertex(Tothree(p2)),
                new SplineVertex(Tothree(p3)),
                new SplineVertex(Tothree(p4))
            };

            /*            List<SplineVertex> ctrlPoints = new List<SplineVertex>
                        {
                            new SplineVertex(new Vector3(0, 0, 0)),
                            new SplineVertex(new Vector3(10, -10, 0)),
                            new SplineVertex(new Vector3(-10, -20, 0)),
                            new SplineVertex(new Vector3(0, -30, 0)),
                        };
            */

            Vector2[] ppp = new Vector2[50];

            for (int i = 0; i < 4; i++)
                ppp[i] = new Vector2(ctrlPoints[i].Position.X, ctrlPoints[i].Position.Y);

            LwPolyline poly = new LwPolyline();

            for (float u = 1.0f; u >= 0.0f; u -= 0.005f)//u的步长决定曲线的疏密
            {
                Vector2 pp = bezier3func(u, ppp);
                poly.Vertexes.Add(new LwPolylineVertex(pp));
            }

            dxf.AddEntity(poly);
        }

        public static void dxfSplineFitPoints(DxfDocument dxf, List<Vector3> point)  //通过点的样条曲线
        {
            // this will be the list of fit points, the resulting spline will pass through them

            // splines crated from a set of fit points are cubic curves (degree 3) and non periodic.
            // to close the spline just repeat the last fit point.
            // note: at the moment splines created this way cannot be used as a boundary path in a hatch, or converted to a polyline or to a list of vertexes,
            // the control point information is not calculated when an spline is created using a list of fit points.
            Spline spline = new Spline(point);
            dxf.AddEntity(spline);
        }



        public static void Singlelinearrow(DxfDocument dxf, string tetx, Vector2 p1, double ang1, Vector2 p2, Vector2 p3, double height, double ArrowSize)//单线箭头
                                                                                                                                                          //p1,p2,p3;分别为箭头顶点，拐角，结尾
                                                                                                                                                          //ang1为箭头朝向方向
        {
            Line line1 = new Line(p3, p2);
            Line line2 = new Line(p2, p1);
            Vector2 v1;
            Vector2 v2;
            Vector2 v3;

            if (ang1 == 180 || ang1 == 0 || ang1 == 90 || ang1 == -90)
            {
                v1 = fun.Polar(fun.Polar(p1, ang1 * Math.PI / 180 - Math.PI, ArrowSize), ang1 * Math.PI / 180 - 90 * Math.PI / 180, ArrowSize * Math.Tan(10 * Math.PI / 180));
                v2 = fun.Polar(fun.Polar(p1, ang1 * Math.PI / 180 - Math.PI, ArrowSize), ang1 * Math.PI / 180 + 90 * Math.PI / 180, ArrowSize * Math.Tan(10 * Math.PI / 180));
                v3 = fun.Polar(fun.Polar(p1, ang1 * Math.PI / 180 - Math.PI, ArrowSize), ang1 * Math.PI / 180, ArrowSize);
            }

            else
            {
                v1 = fun.Polar(fun.Polar(p1, ang1 * Math.PI / 180, ArrowSize), ang1 * Math.PI / 180 - 90 * Math.PI / 180, ArrowSize * Math.Tan(10 * Math.PI / 180));
                v2 = fun.Polar(fun.Polar(p1, ang1 * Math.PI / 180, ArrowSize), ang1 * Math.PI / 180 + 90 * Math.PI / 180, ArrowSize * Math.Tan(10 * Math.PI / 180));
                v3 = fun.Polar(fun.Polar(p1, ang1 * Math.PI / 180, ArrowSize), ang1 * Math.PI / 180 + 180 * Math.PI / 180, ArrowSize);
            }

            Solid entity1 = new Solid(v1, v2, v3);

            Text tet1 = new Text(tetx, fun.Polar(p3, Math.PI / 2, 0.3), height, 0);
            dxf.AddEntity(new EntityObject[] { line1, line2, entity1, tet1 });
        }

        public static void drawBLine(DxfDocument dxf, Vector2[] v2arr, double linewidth, netDxf.Tables.Layer layercolor)
        {
            LwPolyline poly = new LwPolyline();
            for (int i = 0; i < v2arr.Length; i++)
            {
                poly.Vertexes.Add(new LwPolylineVertex(v2arr[i]));
            }
            fun.PolyFill(dxf, poly, linewidth, layercolor);
        }
        public static void drawBline(DxfDocument dxf, Line line1, double linewidth, netDxf.Tables.Layer layercolor)
        {


            Vector2[] v2arr = { new Vector2(line1.StartPoint.X, line1.StartPoint.Y), new Vector2(line1.EndPoint.X, line1.EndPoint.Y) };
            LwPolyline poly = new LwPolyline();
            for (int i = 0; i < v2arr.Length; i++)
            {
                poly.Vertexes.Add(new LwPolylineVertex(v2arr[i]));
            }
            fun.PolyFill(dxf, poly, linewidth, layercolor);
        }


        public static void drawBArc(DxfDocument dxf, Arc arc1, double linewidth, netDxf.Tables.Layer layercolor)
        {
            var center = arc1.Center;
            var c1 = Pus.ToV2(center);
            var startAngle = arc1.StartAngle;
            double endAngle;
            if (arc1.EndAngle < arc1.StartAngle)
            {
                endAngle = arc1.EndAngle + 360;
            }
            else
            {
                endAngle = arc1.EndAngle;
            }
            var radius = arc1.Radius;
            var breaker1 = Pus.AddPointsToLwPolyLine(fun.Polar(c1, startAngle * Math.PI / 180, radius), fun.Polar(c1, endAngle * Math.PI / 180, radius));
            breaker1.Vertexes[0].Bulge = Math.Tan((endAngle - startAngle) * Math.PI / 180 / 4);
            //dxf.AddEntity(breaker1);
            fun.PolyFill(dxf, breaker1, linewidth, layercolor);
        }

        public static void writetext(DxfDocument dxf, string text, Vector2 p1, double height, double angle, double width, string position)
        {
            if (position == "C")
            {
                writetextofC(dxf, text, p1, height, angle, width, 8);
            }
            else if (position == "R")
            {
                writetextofR(dxf, text, p1, height, angle, width, 8);
            }
            else
            {
                writetextofL(dxf, text, p1, height, angle, width, 8);
            }

        }

        public static void writetextofC(DxfDocument dxf, string text, Vector2 p1, double height, double angle, double width, short textcolor)
        {
            TextStyle style = new TextStyle("simsun.ttf");
            Text text1 = new Text(text, Tothree(p1), height, angle, style);
            text1.WidthFactor = width;
            text1.Layer = new Layer("text");
            text1.Layer.Color.Index = textcolor;
            text1.Alignment = TextAlignment.TopCenter;
            dxf.AddEntity(new EntityObject[] { text1 });
        }

        public static void specialcircle(DxfDocument dxf, double centre, double r)
        {
            double x, y, a;

        }

        public static void writetextofL(DxfDocument dxf, string text, Vector2 p1, double height, double angle, double width, short textcolor)
        {
            TextStyle style = new TextStyle("simsun.ttf");
            Text text1 = new Text(text, Tothree(p1), height, angle, style);
            text1.WidthFactor = width;
            text1.Layer = new Layer("text");
            text1.Layer.Color.Index = textcolor;
            text1.Alignment = TextAlignment.TopLeft;
            dxf.AddEntity(new EntityObject[] { text1 });
        }

        public static void writetextofR(DxfDocument dxf, string text, Vector2 p1, double height, double angle, double width, short textcolor)
        {
            TextStyle style = new TextStyle("simsun.ttf");
            Text text1 = new Text(text, Tothree(p1), height, angle, style);
            text1.WidthFactor = width;
            text1.Layer = new Layer("text");
            text1.Layer.Color.Index = textcolor;
            text1.Alignment = TextAlignment.TopRight;
            dxf.AddEntity(new EntityObject[] { text1 });
        }



        public static Vector2 getPointofcir(Vector2 rc1, Vector2 rs1, Vector2 re1, Vector2 rc2, Vector2 rs2, Vector2 re2)  //两段圆弧交点
        {
            double angRr = CalAngle(rc1, rc2);   //两圆心角度
            if (angRr > Math.PI)
            {
                angRr = angRr - Math.PI;
            }
            double dis1 = CalDistance(rc1, rc2);   //两圆心距离
            double r1 = CalDistance(rc1, rs1);   //圆1半径
            double r2 = CalDistance(rc2, rs2);   //圆2半径

            double ang1 = Math.Acos((r1 * r1 + dis1 * dis1 - r2 * r2) / (2 * r1 * dis1));
            double ang2 = Math.Acos((r2 * r2 + dis1 * dis1 - r1 * r1) / (2 * r2 * dis1));

            double angr1s = CalAngle(rc1, rs1);
            double angr1e = CalAngle(rc1, re1);
            double angr2s = CalAngle(rc2, rs2);
            double angr2e = CalAngle(rc2, re2);

            double angf = angRr + ang1;
            double angs = angRr - ang1 + 2 * Math.PI;

            Vector2 p1 = Polar(rc1, angf, r1);
            Vector2 p2 = Polar(rc1, angs, r1);

            if (checkVector(rc1, rs1, re1, p1) && checkVector(rc2, rs2, re2, p1))
            {
                return p1;
            }
            return p2;
        }/*
        public static Vector2 getPointofcir(Arc arc1, Arc arc2)  //两段圆弧交点
        {
            Vector2 rc1, Vector2 rs1, Vector2 re1, Vector2 rc2, Vector2 rs2, Vector2 re2
            var rc1 = new Vector2(arc1.Center.X, arc1.Center.Y);
            var rc2 = new Vector2(arc2.Center.X, arc2.Center.Y);
            var rs1 = new Vector2(arc1.s.X, arc1.Center.Y);
            var rs2 = new Vector2(arc2.Center.X, arc2.Center.Y);

            double angRr = CalAngle(rc1, rc2);   //两圆心角度
            if (angRr > Math.PI)
            {
                angRr = angRr - Math.PI;
            }
            double dis1 = CalDistance(rc1, rc2);   //两圆心距离
            double r1 = CalDistance(rc1, rs1);   //圆1半径
            double r2 = CalDistance(rc2, rs2);   //圆2半径

            double ang1 = Math.Acos((r1 * r1 + dis1 * dis1 - r2 * r2) / (2 * r1 * dis1));
            double ang2 = Math.Acos((r2 * r2 + dis1 * dis1 - r1 * r1) / (2 * r2 * dis1));

            double angr1s = CalAngle(rc1, rs1);
            double angr1e = CalAngle(rc1, re1);
            double angr2s = CalAngle(rc2, rs2);
            double angr2e = CalAngle(rc2, re2);

            double angf = angRr + ang1;
            double angs = angRr - ang1 + 2 * Math.PI;

            Vector2 p1 = Polar(rc1, angf, r1);
            Vector2 p2 = Polar(rc1, angs, r1);

            if (checkVector(rc1, rs1, re1, p1) && checkVector(rc2, rs2, re2, p1))
            {
                return p1;
            }
            return p2;
        }
        */
        public static bool checkVector(Vector2 rc1, Vector2 rs1, Vector2 re1, Vector2 p1)
        {
            double ang1 = CalAngle(rc1, rs1);
            double ang2 = CalAngle(rc1, re1);
            double ang3 = CalAngle(rc1, p1);

            if (ang1 < ang2)
            {
                if (ang3 <= ang2 && ang3 >= ang1)
                {
                    return true;
                }
            }
            if (ang1 > ang2)
            {
                if (ang3 >= ang2 || ang3 <= ang1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 返回二维点顺时针旋转angle度后的新二维点
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Vector2 rotatePoint(Vector2 point, double angle)
        {
            double rad = angle * Math.PI / 180;
            return new Vector2(point.X * Math.Cos(rad) + point.Y * Math.Sin(rad), -point.X * Math.Sin(rad) + point.Y * Math.Cos(rad));
        }

        /// <summary>
        /// 重构rotatePoint方法，默认角度为90°
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Vector2 rotatePoint(Vector2 point)
        {
            return rotatePoint(point, 90);
        }


        #region 点

        public static Vector2 Polar2(Vector2 p1, double length, double angle)//角度制Vector2.Polar
        {
            return Vector2.Polar(p1, length, angle * Math.PI / 180);
        }

        #endregion

        #region 线

        public static LwPolyline AddPointsToLwPolyLine(params Vector2[] points)//多段线
        {
            LwPolyline hPline = new LwPolyline();
            for (int i = 0; i < points.Length; i++)
            {
                if (points[i].X.Equals(double.NaN) || points[i].Y.Equals(double.NaN))
                {
                    throw new Exception("第" + i + "点为非数字值");
                }
                if (points[i] == null)
                {
                    throw new Exception("第" + i + "点为空");
                }
                LwPolylineVertex point = new LwPolylineVertex(points[i]);
                hPline.Vertexes.Add(point);
            }
            return hPline;
        }

        public static LwPolyline MirrorPolyline(LwPolyline lwPolyline, Vector2 startpoint, Vector2 endpoint2)//镜像多线段
        {
            return Pus.MirrorPolyline(lwPolyline, startpoint, endpoint2);
        }

        public static Line MirrorLine(Line line, Vector2 startpoint, Vector2 endpoint2)//镜像线段
        {
            var v1 = new Vector2(line.StartPoint.X, line.StartPoint.Y);
            var v2 = new Vector2(line.EndPoint.X, line.EndPoint.Y);

            var m1 = fun.mirrorpoint(v1, startpoint, endpoint2);
            var m2 = fun.mirrorpoint(v2, startpoint, endpoint2);

            var line1 = new Line(m1, m2);
            return line1;
        }

        public static Arc MirrorArc(Arc arc1, Vector2 pp1, Vector2 pp2)  //镜像 圆弧
        {
            double k, xccz, yccz, xcjx, ycjx, angjxx, angqs1, angzz1;
            if (pp2.X != pp1.X)
            {
                //计算镜向线的斜率
                k = (pp2.Y - pp1.Y) / (pp2.X - pp1.X);
                //计算圆心点垂足坐标
                xccz = (k * k * pp1.X + k * (arc1.Center.Y - pp1.Y) + arc1.Center.X) / (k * k + 1);
                yccz = k * (xccz - pp1.X) + pp1.Y;
                xcjx = arc1.Center.X + 2 * (xccz - arc1.Center.X);
                ycjx = arc1.Center.Y + 2 * (yccz - arc1.Center.Y);
                //计算镜向后起始及终止角
                angjxx = Math.Atan(k) * 180 / 3.1415926535897;
                angzz1 = 2 * angjxx - arc1.StartAngle;
                angqs1 = 2 * angjxx - arc1.EndAngle;
            }
            else
            {
                //计算直线起点垂足坐标
                xccz = pp1.X;
                yccz = arc1.Center.Y;
                xcjx = arc1.Center.X + 2 * (xccz - arc1.Center.X);
                ycjx = arc1.Center.Y + 2 * (yccz - arc1.Center.Y);
                //计算镜向后起始及终止角
                angjxx = 90;
                angzz1 = 2 * angjxx - arc1.StartAngle;
                angqs1 = 2 * angjxx - arc1.EndAngle;

            }
            Arc arc1p = new Arc(new Vector2(xcjx, ycjx), arc1.Radius, angqs1, angzz1);
            arc1p.Layer = arc1.Layer;
            return arc1p;
        }

        public static Circle MirrorCircle(Circle circle1, Vector2 pp1, Vector2 pp2)  //镜像 圆
        {
            double k, xccz, yccz, xcjx, ycjx;
            if (pp2.X != pp1.X)
            {
                //计算镜向线的斜率
                k = (pp2.Y - pp1.Y) / (pp2.X - pp1.X);
                //计算圆心点垂足坐标
                xccz = (k * k * pp1.X + k * (circle1.Center.Y - pp1.Y) + circle1.Center.X) / (k * k + 1);
                yccz = k * (xccz - pp1.X) + pp1.Y;
                xcjx = circle1.Center.X + 2 * (xccz - circle1.Center.X);
                ycjx = circle1.Center.Y + 2 * (yccz - circle1.Center.Y);
            }
            else
            {
                //计算直线起点垂足坐标
                xccz = pp1.X;
                yccz = circle1.Center.Y;
                xcjx = circle1.Center.X + 2 * (xccz - circle1.Center.X);
                ycjx = circle1.Center.Y + 2 * (yccz - circle1.Center.Y);
            }
            Circle circle1p = new Circle(new Vector2(xcjx, ycjx), circle1.Radius);
            circle1p.Layer = circle1.Layer;
            return circle1p;
        }



        #endregion


        #region 填充
        public static void AddHatch(DxfDocument dxf, Layer layer, LwPolyline lw, Line[] lines, Arc[] arcs, double rotation, double distance)//添加填充
        {

            String[] htype1 = new String[100];
            int hnumst1 = lw.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = lw.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    lines[i] = (Line)lw.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    arcs[i] = (Arc)lw.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, rotation, distance, lines, arcs, layer);
        }

        public static void Hatch(DxfDocument dxf, Layer layer, double rotation, double scale, params EntityObject[] entities)//填充，netdxf的填充对象网页无法显示
        {
            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath> { new HatchBoundaryPath(entities) };
            Hatch hatch = new Hatch(HatchPattern.Line, boundary, true);
            hatch.Pattern.Angle = rotation;
            hatch.Pattern.Scale = scale;
            hatch.Layer = layer;

            dxf.AddEntity(hatch);
        }


        #endregion


        #region 标注

        public static Block OutsideClosedfilled//外箭头
        {
            get
            {
                Block arrowhead = new Block("_OBLIQUE");

                Solid sd = new Solid(new Vector2(1.0, 0.1666666666666666), new Vector2(0.0, 0.0), new Vector2(1.0, -0.1666666666666666))
                {
                    Layer = Layer.Default,
                    Linetype = Linetype.ByBlock,
                    Color = AciColor.ByBlock,
                    Lineweight = Lineweight.ByBlock
                };

                Line line = new Line(new Vector2(2.0, 0), new Vector2(0.0, 0.0))
                {
                    Layer = Layer.Default,
                    Linetype = Linetype.ByBlock,
                    Color = AciColor.ByBlock,
                    Lineweight = Lineweight.ByBlock
                };

                arrowhead.Entities.Add(sd);
                arrowhead.Entities.Add(line);


                return arrowhead;
            }
        }

        public static DimensionStyle DimStyle(bool arrowDirection)//内外箭头标注样式，true为内箭头，false为外箭头
        {
            double textHeight = 3.5;

            DimensionStyle ds1 = new DimensionStyle("ds1");
            ds1.ArrowSize = textHeight;
            ds1.TextHeight = textHeight;
            ds1.ExtLineExtend = textHeight / 2;
            ds1.TextOffset = textHeight / 4;
            ds1.DimArrow1 = DimensionArrowhead.Closedfilled;//内箭头
            ds1.DimArrow2 = DimensionArrowhead.Closedfilled;

            DimensionStyle ds2 = new DimensionStyle("ds2");
            ds2.ArrowSize = textHeight;
            ds2.TextHeight = textHeight;
            ds2.ExtLineExtend = textHeight / 2;
            ds2.TextOffset = textHeight / 4;
            ds2.DimArrow1 = fun.OutsideClosedfilled;
            ds2.DimArrow2 = fun.OutsideClosedfilled;//外箭头

            return arrowDirection ? ds1 : ds2;

        }

        public static void DimensionLiner(DxfDocument dxf, Layer layer, Vector2 v1, Vector2 v2, double offset, double rotation, string text)//直线标注
        {
            DimensionStyle ds1 = fun.DimStyle(true);
            DimensionStyle ds2 = fun.DimStyle(false);
            double arrowSize = ds1.ArrowSize;
            double ang = rotation * Math.PI / 180;
            bool isFlip = Math.Round(Math.Cos(ang), 11) > 0 || ((Math.Round(Math.Cos(ang), 11) == 0 && Math.Sin(ang) > 0));


            LinearDimension ld = new LinearDimension(v1, v2, offset, rotation, ds1);
            if (ld.Measurement < arrowSize * 3)
                ld.Style = ds2;
            ld.UserText = text;

            string spaces = new string(' ', (int)Math.Ceiling(ld.Measurement / arrowSize) + text.Length) + '\t';
            if (text.Length > ld.Measurement / arrowSize)
            {
                ld.UserText = isFlip ? (spaces + text) : (text + spaces);
                Vector2 startPoint = fun.Polar2(Vector2.MidPoint(v1, v2), offset, rotation + 90);
                Vector2 endPoint = fun.Polar2(startPoint, ld.UserText.Length * arrowSize / 2, rotation);
                Line line = new Line(startPoint, endPoint);
                fun.AddEntities(dxf, layer, line);
            }

            fun.AddEntities(dxf, layer, ld);
        }

        public static void DimensionLiner(DxfDocument dxf, Vector2 v1, Vector2 v2, double offset, double rotation, string text)//直线标注
        {
            double jj = 1;
            double height = 3.5;
            double arrowSize = height;
            double exline = height / 2;
            string toh = "";
            string tol = "";

            fun.dimrot1(dxf, v1, v2, rotation, offset, jj, height, arrowSize, exline, text, toh, tol);
        }

        public static void DimensionLiner(DxfDocument dxf, Vector2 v1, Vector2 v2, double offset, double rotation, string text, string toh, string tol)//直线标注
        {
            double jj = 1;
            double height = 3.5;
            double arrowSize = height;
            double exline = height / 2;
            fun.dimrot1(dxf, v1, v2, rotation, offset, jj, height, arrowSize, exline, text, toh, tol);
        }

        public static void DimensionAligned(DxfDocument dxf, Layer layer, Vector2 v1, Vector2 v2, double offset, string text)//对齐标注
        {
            DimensionStyle ds1 = fun.DimStyle(true);
            DimensionStyle ds2 = fun.DimStyle(false);
            double arrowSize = ds1.ArrowSize;
            double ang = Vector2.Angle(v1, v2);
            double rotation = ang * 180 / Math.PI;
            bool isFlip = Math.Round(Math.Cos(ang), 11) > 0 || ((Math.Round(Math.Cos(ang), 11) == 0 && Math.Sin(ang) > 0));


            AlignedDimension ad = new AlignedDimension(v1, v2, offset, ds1);
            if (ad.Measurement < arrowSize * 3)
                ad.Style = ds2;
            ad.UserText = text;

            string spaces = new string(' ', (int)Math.Ceiling(ad.Measurement / arrowSize) + text.Length) + '\t';
            if (text.Length > ad.Measurement / arrowSize)
            {
                ad.UserText = isFlip ? (spaces + text) : (text + spaces);
                Vector2 startPoint = fun.Polar2(Vector2.MidPoint(v1, v2), offset, rotation + 90 + (isFlip ? 0 : 180));
                Vector2 endPoint = fun.Polar2(startPoint, ad.UserText.Length * arrowSize / 2, rotation);
                Line line = new Line(startPoint, endPoint);
                fun.AddEntities(dxf, layer, line);
            }

            fun.AddEntities(dxf, layer, ad);
        }

        public static void DimensionAngular(DxfDocument dxf, Layer layer, Line line1, Line line2, double offset, string text)//角度标注
        {
            DimensionStyle ds1 = fun.DimStyle(true);
            DimensionStyle ds2 = fun.DimStyle(false);

            Angular2LineDimension ad = new Angular2LineDimension(line1, line2, offset, ds1);
            if (ad.Measurement < 40)
            {
                ad.Style = ds2;

            }
            ad.UserText = text;
            ad.Layer = layer;

            dxf.AddEntity(ad);
        }

        public static void DimensionAngular(DxfDocument dxf, Line line1, Line line2, double offset, string text)//角度标注
        {

            double height = 3.5;
            double arrowSize = height;
            double exline = height / 2;
            string toh = "";
            string tol = "";
            fun.jdbz2(dxf, line1, line2, offset, height, arrowSize, exline, text, toh, tol);
        }

        public static void DimensionAngular(DxfDocument dxf, Line line1, Line line2, double offset, string text, string toh, string tol)//角度标注
        {
            double height = 3.5;
            double arrowSize = height;
            double exline = height / 2;
            fun.jdbz2(dxf, line1, line2, offset, height, arrowSize, exline, text, toh, tol);
        }

        public static void DimensionRadius(DxfDocument dxf, Layer layer, Arc arc, double rotation, double offset, string text)//半径标注
        {
            DimensionStyle ds1 = fun.DimStyle(true);
            DimensionStyle ds2 = fun.DimStyle(false);
            RadialDimension rd = new RadialDimension(arc, rotation, offset, ds1);
            rd.UserText = text;
            rd.Layer = layer;

            dxf.AddEntity(rd);
        }

        public static void DimensionDiameter(DxfDocument dxf, Layer layer, Circle circle, double rotation, double offset, string text)//直径标注
        {
            DimensionStyle ds1 = fun.DimStyle(true);
            DimensionStyle ds2 = fun.DimStyle(false);
            DiametricDimension dd = new DiametricDimension(circle, rotation, offset, ds1);
            dd.Layer = layer;
            dd.UserText = text;

            dxf.AddEntity(dd);
        }

        public static void DimensionTaper(DxfDocument dxf, Vector2 point, double ang1, double len1, double len2, string text)//锥度标注
        {
            fun.jtbz(dxf, point, ang1, len1, len2, text, 3.5, 3.5);
        }

        #endregion

        #region 图层

        public static Layer SetLayer(string s)//设置图层属性,返回图层对象
        {

            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层



            if (s == "contour")
            {
                // 设置图层 "contour"

                contour.Color = AciColor.Cyan;
                contour.Lineweight = Lineweight.W50;
                contour.Linetype = Linetype.Continuous;

                return contour;
            }
            if (s == "dimensioning")
            {
                // 设置图层 "dimensioning"
                dimensioning.Color = AciColor.Default;
                dimensioning.Lineweight = Lineweight.W25;
                dimensioning.Linetype = Linetype.Continuous;

            }
            if (s == "refin")
            {

                // 设置图层 "refin"
                refin.Color = AciColor.Red;
                refin.Lineweight = Lineweight.W35;
                refin.Linetype = Linetype.Dashed;


                return refin;

            }
            if (s == "center")
            {
                // 设置图层 "center"
                center.Color = AciColor.Yellow;
                center.Lineweight = Lineweight.W25;
                center.Linetype = Linetype.DashDot;


                return center;

            }
            if (s == "textlayer")
            {
                // 设置图层 "textlayer"
                textlayer.Color = AciColor.Default;
                textlayer.Lineweight = Lineweight.W25;
                textlayer.Linetype = Linetype.Continuous;


                return textlayer;

            }
            if (s == "hatching")
            {
                // 设置图层 "hatching"
                hatching.Color = AciColor.Blue;
                hatching.Lineweight = Lineweight.W25;
                hatching.Linetype = Linetype.Continuous;



                return hatching;

            }
            if (s == "additionalline")
            {
                // 设置图层 "additionalline"

                additionalline.Color = AciColor.Cyan;
                additionalline.Lineweight = Lineweight.W25;
                additionalline.Linetype = Linetype.Continuous;

                return additionalline;

            }
            if (s == "cutting")
            {
                // 设置图层 "cutting"
                cutting.Color = AciColor.Blue;
                cutting.Lineweight = Lineweight.W25;
                cutting.Linetype = Linetype.Continuous;


                return cutting;

            }

            // 设置默认图层 "0"
            Layer.Default.Color = AciColor.Default;
            Layer.Default.Lineweight = Lineweight.W25;
            Layer.Default.Linetype = Linetype.Continuous;

            return Layer.Default;





        }

        public static void SetLayer(DxfDocument dxf)//设置所有实体的图层属性，不返回对象
        {

            // 设置图层 "contour"
            if (dxf.Layers.Contains("contour"))
            {
                dxf.Layers["contour"].Color = AciColor.Cyan;
                dxf.Layers["contour"].Lineweight = Lineweight.W50;
                dxf.Layers["contour"].Linetype = Linetype.Continuous;
            }

            // 设置图层 "dimensioning"
            if (dxf.Layers.Contains("dimensioning"))
            {
                dxf.Layers["dimensioning"].Color = AciColor.Default;
                dxf.Layers["dimensioning"].Lineweight = Lineweight.W25;
                dxf.Layers["dimensioning"].Linetype = Linetype.Continuous;
            }

            // 设置图层 "refin"
            if (dxf.Layers.Contains("refin"))
            {
                dxf.Layers["refin"].Color = AciColor.Red;
                dxf.Layers["refin"].Lineweight = Lineweight.W35;
                dxf.Layers["refin"].Linetype = Linetype.Dashed;
            }

            // 设置图层 "center"
            if (dxf.Layers.Contains("center"))
            {
                dxf.Layers["center"].Color = AciColor.Yellow;
                dxf.Layers["center"].Lineweight = Lineweight.W25;
                dxf.Layers["center"].Linetype = Linetype.DashDot;
            }

            // 设置图层 "textlayer"
            if (dxf.Layers.Contains("textlayer"))
            {
                dxf.Layers["textlayer"].Color = AciColor.Default;
                dxf.Layers["textlayer"].Lineweight = Lineweight.W25;
                dxf.Layers["textlayer"].Linetype = Linetype.Continuous;
            }

            // 设置图层 "hatching"
            if (dxf.Layers.Contains("hatching"))
            {
                dxf.Layers["hatching"].Color = AciColor.Blue;
                dxf.Layers["hatching"].Lineweight = Lineweight.W25;
                dxf.Layers["hatching"].Linetype = Linetype.Continuous;
            }

            // 设置图层 "additionalline"
            if (dxf.Layers.Contains("additionalline"))
            {
                dxf.Layers["additionalline"].Color = AciColor.Cyan;
                dxf.Layers["additionalline"].Lineweight = Lineweight.W25;
                dxf.Layers["additionalline"].Linetype = Linetype.Continuous;
            }

            // 设置图层 "cutting"
            if (dxf.Layers.Contains("cutting"))
            {
                dxf.Layers["cutting"].Color = AciColor.Blue;
                dxf.Layers["cutting"].Lineweight = Lineweight.W25;
                dxf.Layers["cutting"].Linetype = Linetype.Continuous;
            }

            // 设置默认图层 "0"
            if (dxf.Layers.Contains("0"))
            {
                dxf.Layers["0"].Color = AciColor.Default;
                dxf.Layers["0"].Lineweight = Lineweight.W25;
                dxf.Layers["0"].Linetype = Linetype.Continuous;
            }

        }

        public static void SetByLayer(DxfDocument dxf)//设置所有实体跟随图层

        {
            foreach (Line line in dxf.Lines)
            {
                line.Color = AciColor.ByLayer;
                line.Linetype = Linetype.ByLayer;
                line.Lineweight = Lineweight.ByLayer;
                line.LinetypeScale = 1;
            }
            foreach (Arc arc in dxf.Arcs)
            {
                arc.Color = AciColor.ByLayer;
                arc.Linetype = Linetype.ByLayer;
                arc.Lineweight = Lineweight.ByLayer;
                arc.LinetypeScale = 1;
            }
            foreach (Circle circle in dxf.Circles)
            {
                circle.Color = AciColor.ByLayer;
                circle.Linetype = Linetype.ByLayer;
                circle.Lineweight = Lineweight.ByLayer;
                circle.LinetypeScale = 1;
            }
            foreach (Ellipse ellipse in dxf.Ellipses)
            {
                ellipse.Color = AciColor.ByLayer;
                ellipse.Linetype = Linetype.ByLayer;
                ellipse.Lineweight = Lineweight.ByLayer;
                ellipse.LinetypeScale = 1;
            }
            foreach (LwPolyline lwPolyline in dxf.LwPolylines)
            {
                lwPolyline.Color = AciColor.ByLayer;
                lwPolyline.Linetype = Linetype.ByLayer;
                lwPolyline.Lineweight = Lineweight.ByLayer;
                lwPolyline.LinetypeScale = 1;
            }
            foreach (Polyline polyline in dxf.Polylines)
            {
                polyline.Color = AciColor.ByLayer;
                polyline.Linetype = Linetype.ByLayer;
                polyline.Lineweight = Lineweight.ByLayer;
                polyline.LinetypeScale = 1;
            }
            foreach (MText mText in dxf.MTexts)
            {
                mText.Color = AciColor.ByLayer;
                mText.Linetype = Linetype.ByLayer;
                mText.Lineweight = Lineweight.ByLayer;
                mText.LinetypeScale = 1;

                mText.Height = 3.5;
                mText.Style = new TextStyle("长仿宋体.ttf");
            }
            foreach (Text text in dxf.Texts)
            {
                text.Color = AciColor.ByLayer;
                text.Linetype = Linetype.ByLayer;
                text.Lineweight = Lineweight.ByLayer;
                text.LinetypeScale = 1;

                text.Height = 3.5;
                text.Style = new TextStyle("长仿宋体.ttf");
            }


            foreach (Block block in dxf.Blocks)
            {
                foreach (EntityObject entity in block.Entities)
                {

                    // 检查是否为单行文本对象
                    if (entity is Text textEntity)
                    {
                        textEntity.Color = AciColor.ByLayer;
                        textEntity.Linetype = Linetype.ByLayer;
                        textEntity.Lineweight = Lineweight.ByLayer;
                        textEntity.LinetypeScale = 1;

                        textEntity.Height = 3.5;
                        textEntity.Style = new TextStyle("长仿宋体.ttf");
                    }
                    // 检查是否为多行文本对象
                    else if (entity is MText mTextEntity)
                    {
                        mTextEntity.Color = AciColor.ByLayer;
                        mTextEntity.Linetype = Linetype.ByLayer;
                        mTextEntity.Lineweight = Lineweight.ByLayer;
                        mTextEntity.LinetypeScale = 1;

                        mTextEntity.Height = 3.5;
                        mTextEntity.Style = new TextStyle("长仿宋体.ttf");
                    }
                }
            }


        }


        #endregion

        #region 编辑

        public static void SetTexts(DxfDocument dxf)//批量设置文本属性
        {
            foreach (Text text in dxf.Texts)
            {
                text.Color = AciColor.ByLayer;
                text.Linetype = Linetype.ByLayer;
                text.Lineweight = Lineweight.ByLayer;
                text.LinetypeScale = 1;

                text.Height = 3.5;
                text.Style = new TextStyle("长仿宋体.ttf");
            }

            foreach (MText mText in dxf.MTexts)
            {
                mText.Color = AciColor.ByLayer;
                mText.Linetype = Linetype.ByLayer;
                mText.Lineweight = Lineweight.ByLayer;
                mText.LinetypeScale = 1;

                mText.Height = 3.5;
                mText.Style = new TextStyle("长仿宋体.ttf");
            }

            foreach (Block block in dxf.Blocks)
            {
                foreach (EntityObject entity in block.Entities)
                {

                    // 检查是否为单行文本对象
                    if (entity is Text textEntity)
                    {
                        textEntity.Color = AciColor.ByLayer;
                        textEntity.Linetype = Linetype.ByLayer;
                        textEntity.Lineweight = Lineweight.ByLayer;
                        textEntity.LinetypeScale = 1;

                        textEntity.Height = 3.5;
                        textEntity.Style = new TextStyle("长仿宋体.ttf");
                    }
                    // 检查是否为多行文本对象
                    else if (entity is MText mTextEntity)
                    {
                        mTextEntity.Color = AciColor.ByLayer;
                        mTextEntity.Linetype = Linetype.ByLayer;
                        mTextEntity.Lineweight = Lineweight.ByLayer;
                        mTextEntity.LinetypeScale = 1;

                        mTextEntity.Height = 3.5;
                        mTextEntity.Style = new TextStyle("长仿宋体.ttf");
                    }
                }
            }
        }

        public static void AddEntities(DxfDocument dxf, Layer layer, params EntityObject[] entities)//批量添加实体并设置图层
        {
            foreach (var entity in entities)
            {
                entity.Layer = layer;

                dxf.AddEntity(entity);
            }
        }

        public static LwPolyline RotateLwPolyline(LwPolyline lwPolyline, Vector2 center, double angle)//旋转多线段
        {
            // 将角度转换为弧度
            double radians = angle * Math.PI / 180.0;

            // 创建一个新的 LwPolyline 来存储旋转后的结果
            LwPolyline rotatedPolyline = new LwPolyline();

            // 遍历原始 LwPolyline 的所有顶点
            foreach (var vertex in lwPolyline.Vertexes)
            {
                // 获取当前顶点的坐标
                Vector2 originalPoint = new Vector2(vertex.Position.X, vertex.Position.Y);

                // 计算旋转后的坐标
                double translatedX = originalPoint.X - center.X;
                double translatedY = originalPoint.Y - center.Y;

                double rotatedX = translatedX * Math.Cos(radians) - translatedY * Math.Sin(radians) + center.X;
                double rotatedY = translatedX * Math.Sin(radians) + translatedY * Math.Cos(radians) + center.Y;

                // 创建新的顶点并添加到旋转后的 LwPolyline
                LwPolylineVertex rotatedVertex = new LwPolylineVertex(new Vector2(rotatedX, rotatedY), vertex.Bulge);
                rotatedPolyline.Vertexes.Add(rotatedVertex);
            }

            // 保留原始 LwPolyline 的闭合状态
            rotatedPolyline.IsClosed = lwPolyline.IsClosed;

            return rotatedPolyline;
        }

        public static LwPolyline[] CircularArrayLwPolyline(LwPolyline lwPolyline, Vector2 center, int count)//圆形阵列多线段
        {
            // 创建一个数组存储阵列后的多线段
            LwPolyline[] arrayedPolylines = new LwPolyline[count];

            // 计算每个多线段之间的角度间隔
            double angleStep = 360.0 / count;

            // 计算原始多线段的中心点
            Vector2 polylineCenter = CalculatePolylineCenter(lwPolyline);

            // 遍历阵列数量
            for (int i = 0; i < count; i++)
            {
                // 计算当前多线段的旋转角度
                double angle = i * angleStep;

                // 计算当前多线段的中心点位置
                Vector2 newCenter = RotatePointAroundCenter(polylineCenter, center, angle);

                // 创建一个新的 LwPolyline
                LwPolyline newPolyline = new LwPolyline();

                // 遍历原始多线段的顶点
                foreach (var vertex in lwPolyline.Vertexes)
                {
                    // 计算顶点相对于原始中心的偏移
                    Vector2 offset = vertex.Position - polylineCenter;

                    // 旋转顶点并平移到新中心
                    Vector2 rotatedPosition = RotatePointAroundCenter(offset, Vector2.Zero, angle) + newCenter;

                    // 创建新的顶点并添加到新多线段
                    newPolyline.Vertexes.Add(new LwPolylineVertex(rotatedPosition, vertex.Bulge));
                }

                // 保留原始多线段的闭合状态
                newPolyline.IsClosed = lwPolyline.IsClosed;

                // 将新多线段添加到数组
                arrayedPolylines[i] = newPolyline;
            }

            return arrayedPolylines;
        }

        private static Vector2 CalculatePolylineCenter(LwPolyline lwPolyline)// 辅助方法：计算多线段的中心点
        {
            double sumX = 0;
            double sumY = 0;

            foreach (var vertex in lwPolyline.Vertexes)
            {
                sumX += vertex.Position.X;
                sumY += vertex.Position.Y;
            }

            return new Vector2(sumX / lwPolyline.Vertexes.Count, sumY / lwPolyline.Vertexes.Count);
        }

        public static Vector2 RotatePointAroundCenter(Vector2 point, Vector2 center, double angle)// 辅助方法：绕中心点旋转一个点
        {
            // 将角度转换为弧度
            double radians = angle * Math.PI / 180.0;

            // 平移点到原点
            double translatedX = point.X - center.X;
            double translatedY = point.Y - center.Y;

            // 旋转公式
            double rotatedX = translatedX * Math.Cos(radians) - translatedY * Math.Sin(radians);
            double rotatedY = translatedX * Math.Sin(radians) + translatedY * Math.Cos(radians);

            // 平移回原位置
            return new Vector2(rotatedX + center.X, rotatedY + center.Y);
        }

        public static Line RotateLine(Line line, Vector2 center, double angle)// 旋转直线
        {
            // 获取直线的起点和终点
            Vector2 startPoint = new Vector2(line.StartPoint.X, line.StartPoint.Y);
            Vector2 endPoint = new Vector2(line.EndPoint.X, line.EndPoint.Y);

            // 旋转起点和终点
            Vector2 rotatedStart = RotatePointAroundCenter(startPoint, center, angle);
            Vector2 rotatedEnd = RotatePointAroundCenter(endPoint, center, angle);

            // 返回旋转后的直线
            return new Line(rotatedStart, rotatedEnd);
        }

        public static void DrawFrame(DxfDocument dxf, Dictionary<string, Object> data, double scale, Vector2 p0)//图框
        {
            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }
        }
        /*
                public static List<Line> Trim(DxfDocument dxf, Line boundary1, Line boundary2)//修剪线段
                {

                    //获取所有实体线条
                    List<Line> lines = new List<Line>();
                    foreach (var entity in dxf.Lines)
                    {



                    }


                    return lines;
                }

                /// <summary>
                /// 计算两条直线的交点
                /// </summary>
                /// <param name="p1">第一条直线的起点</param>
                /// <param name="p2">第一条直线的终点</param>
                /// <param name="p3">第二条直线的起点</param>
                /// <param name="p4">第二条直线的终点</param>
                /// <returns>交点坐标，如果平行或重合则返回 null</returns>
                public static Vector2? GetIntersection(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
                {



                    // 计算两条直线的方向向量
                    double a1 = p2.Y - p1.Y;
                    double b1 = p1.X - p2.X;
                    double c1 = a1 * p1.X + b1 * p1.Y;

                    double a2 = p4.Y - p3.Y;
                    double b2 = p3.X - p4.X;
                    double c2 = a2 * p3.X + b2 * p3.Y;

                    // 计算行列式
                    double determinant = a1 * b2 - a2 * b1;

                    // 如果行列式为 0，说明两直线平行或重合
                    if (Math.Abs(determinant) < 1e-10)
                    {
                        return null;
                    }

                    // 计算交点
                    double x = (b2 * c1 - b1 * c2) / determinant;
                    double y = (a1 * c2 - a2 * c1) / determinant;

                    double xMin = (p1.X < p2.X) ? p1.X : p2.X;
                    double xMax = (p1.X > p2.X) ? p1.X : p2.X;
                    double yMin = (p1.Y < p2.Y) ? p1.Y : p2.Y;
                    double yMax = (p1.Y > p2.Y) ? p1.Y : p2.Y;


                    if (x >= xMin && x <= xMax && y >= yMin && y <= yMax)
                    {
                        return new Vector2(x, y);
                    }
                    return null;

                }*/

         

         

    #endregion




}




    /*
    // *** ARC ***
    const R = 100; // RADIUS
    const PHI = 180; // ANGLE (DEG)

    // *** LINES ***
    const LINES = 10; // NUMBER OF LINES TO BE PLACED

    // *** CALCULATIONS ***
    const coords = getLinesCoords(R, PHI, LINES);

    console.log(coords);
    */

    //  直线和圆的

    public class Param  //直线方程的三个参数
    {
        public double A { get; set; }
        public double B { get; set; }
        public double C { get; set; }

        public Param(double a, double b, double c)
        {
            this.A = a;
            this.B = b;
            this.C = c;
        }

        public static Param CalParam(Vector2 p1, Vector2 p2)  //两点所连直线的方程
        {
            double a, b, c;
            double x1 = p1.X, y1 = p1.Y, x2 = p2.X, y2 = p2.Y;
            a = y2 - y1;
            b = x1 - x2;
            c = (x2 - x1) * y1 - (y2 - y1) * x1;
            if (b < 0)
            {
                a *= -1; b *= -1; c *= -1;
            }
            else if (b == 0 && a < 0)
            {
                a *= -1; c *= -1;
            }
            return new Param(a, b, c);
        }

        private static double distance(Vector3 pp1, Vector2 pp2)
        {
            double dist;
            dist = Math.Sqrt(Math.Pow((pp2.X - pp1.X), 2) + Math.Pow((pp2.Y - pp1.Y), 2));
            return dist;
        }
        private static double distance1(Vector2 pp1, Vector2 pp2)
        {
            double dist;
            dist = Math.Sqrt(Math.Pow((pp2.X - pp1.X), 2) + Math.Pow((pp2.Y - pp1.Y), 2));
            return dist;
        }



    }

}


